7

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.

Community
  • 1
  • 1
Joshua Rosenberg
  • 4,014
  • 9
  • 34
  • 73
  • 4
    your problem is with the `load` function. Instead of `df <- load("~/downloads/df.Rda")`, use `load("~/downloads/df.Rda")` – Alex Jun 17 '15 at 01:41
  • 3
    @Alex has it - you don't need to assign when doing `load`, it will just create the object you saved in the `parent.frame()` environment - in this case the global workspace. The reason why you get `"df"` in text returned is because, as `?load` says, the function returns: `A character vector of the names of objects created, invisibly.` The saved data should still have been loaded into `df` though. – thelatemail Jun 17 '15 at 01:45
  • I see, ty for the help – Joshua Rosenberg Jun 17 '15 at 01:46
  • Is there a smart way to assign while loading though? This always gets me as well. Thanks for the explanation @thelatemail. – Alex Jun 17 '15 at 01:46
  • 2
    @Alex - maybe - `tmpenv <- new.env(); load("df.Rda",envir=tmpenv); newdf <- tmpenv[["df"]]` or something similar. – thelatemail Jun 17 '15 at 01:58
  • Other useful suggestions via: http://stackoverflow.com/search?q=[r]+load+environment+is%3Aanswer – thelatemail Jun 17 '15 at 03:28

1 Answers1

6

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.

Community
  • 1
  • 1
Carlos Cinelli
  • 11,354
  • 9
  • 43
  • 66