According to the answer to this question, you can save a data frame "foo" in R with the save() function as follows:
save(foo,file="data.Rda")
Here is data frame "df":
> str(df)
'data.frame': 1254 obs. of 2 variables
$ text : chr "RT @SchmittySays: I love this 1st grade #science teacher from #Duluth http://t.co/HWDYFnIyqV #NSTA15 #AlbertEinstein #inspirat"| __truncated__ "RT @KVernonBHS: @smrtgrls would love Stellar Girls. Empowering female scientists rocks! #NSTA15 http://t.co/1ZU0yjVF67" "RT @leducmills: Leaving #SXSWedu to go straight to #NSTA15. There should be some sort of arbitrary conference-hopper social med"| __truncated__ "RT @KRScienceLady: Congrats to a wonderful colleague who helped #ngss Bcome reality, Stephen Pruitt, Distinguished Service to "| __truncated__ ...
$ group: Factor w/ 2 levels "narst","nsta": 2 2 2 2 2 2 2 2 2 2 ...
It seems to save fine:
> save(df, file = "~/downloads/df.Rda")
But it turns out only the name of the object saved:
> df1 <- load("~/downloads/df.Rda")
> str(df1)
chr "df"
I tried the saveRDS() function suggested in another answer to the same question referenced above which worked fine, but I'd like to know why save() isn't working.
You can save an R object like a data frame as either an RData file or an RDS file. RData files can store multiple R objects at once, but RDS files are the better choice because they foster reproducible code. To save data as an RData object, use the save function. To save data as a RDS object, use the saveRDS function.
R dataset files One of the simplest ways to save your data is by saving it into an RData file with the function save( ). R saves your data to the working folder on your computer disk in a binary file.
save writes an external representation of R objects to the specified file. The objects can be read back from the file at a later date by using the function load or attach (or data in some cases).
You might want to take a look at this question here: R data formats: RData, Rda, Rds etc.
When loading an .rda
object, you are going to load all objects with their original names to the global environment. You can't assign objects to new names using load
as you tried to do.
If you want to save objects that can be loaded with different names later, then you should use the .rds
format (saveRDS
and readRDS
). If you want to save more than one object in a .rds
file, the simplest solution is to put all of them on a list and save only the list. If after reading the .rds
you want to put the objects of the list in the global environment, you can use list2env
.
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