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.
Use str_replace() method from stringr package to replace part of a column string with another string in R DataFrame.
Method 1: Using colnames() function colnames() function in R is used to set headers or names to columns of a dataframe or matrix. Syntax: colnames(dataframe) <- c(“col_name-1”, “col_name-2”, “col_name-3”, “col_name-4”,…..)
To save data as an RData object, use the save function. To save data as a RDS object, use the saveRDS function. In each case, the first argument should be the name of the R object you wish to save. You should then include a file argument that has the file name or file path you want to save the data set to.
Add x
to the list
argument from save()
. From the help file:
list A character vector containing the names of objects to be saved.
save(list=x, file='somefilename.rda')
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')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With