Alex Rivera | Logout

save an R dataframe with the name specified by a string

Asked 2011-06-11T19:52:59.930
10

On this awesome forum I saw a post which shows how to convert a string to a variable and assign a data frame to that variable. For example:

x = "thisisthestring"

# df is a data frame
assign(x, df) # This will assign data frame df to variable thisisthestring

What I want to do is save this data frame with the name thisisthestring. However, if I try

assign(x, df)
save(x, file='somefilename.rda') 

the file just contains a string "thisisthestring" and not the data frame df.

I also tried

save(assign(x, df), file = 'somefile.rda'))

That does not work either. Any suggestions how I can save the data frame to a file, where the name of the data frame is specified by the string.

r
Edit
Report

1 Answer

6

You want to pass x as the argument list to the save() function, not as part of argument ... (the first argument of save()). This should work:

save(list = x, file='somefilename.rda')
answered 2011-06-11T20:20:36.547

Your Answer