Charles
Charles

Reputation: 4168

converting a string into a data frame name

In functions such as plotmeans there is an argument that specifies the data frame to use, data=. I would like to construct the name of the data frame to be used using paste0 or something similar, df <- paste0("results", i), where i is a number to get (say) "results04". If I then use data=df, I get an error saying that data= expects a variable, not a string. Is there any way to convert the string into a form that data= will accept? data=results04 without the quotes, of course, works.

Thanks for any suggestions or pointers.

Upvotes: 4

Views: 5433

Answers (1)

Charles
Charles

Reputation: 4168

The answer would have been obvious to one with more R experience, but let me put it here for others: use the get() function, so for instance

df <- paste0("results", i)
plotmeans(a ~ b, data=get(df))

Upvotes: 8

Related Questions