How to save a data frame in R

According to the answer to this question, you can save the “foo” data frame in R using the save () function as follows:

save(foo,file="data.Rda") 

Here is the "df" data frame:

 > 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 he will save a fine:

 > save(df, file = "~/downloads/df.Rda") 

But only the name of the saved object is obtained:

 > df1 <- load("~/downloads/df.Rda") > str(df1) chr "df" 

I tried the saveRDS () function suggested in another answer to the same question that was referenced above, which worked fine, but I would like to know why save () does not work.

+6
source share
1 answer

You can take a look at this question here: R data formats: RData, Rda, Rds, etc. .

When you load a .rda object .rda you load all the objects with their original names into the global environment. You cannot 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, you must use the .rds format ( saveRDS and readRDS ). If you want to save more than one object in a .rds file, the easiest solution is to put all of them in a list and save only the list. If after reading .rds you want to put list objects in a global environment, you can use list2env .

+5
source

Source: https://habr.com/ru/post/989258/


All Articles