Saving and loading data.frames

Layla picture Layla · Nov 3, 2012 · Viewed 56k times · Source

I have made a dataframe based on a set of twitters in the following form:

 rdmTweets <- userTimeline("rdatamining", n=200)
 df <- do.call("rbind", lapply(rdmTweets, as.data.frame))

Now I am saving the data frame with save in this way:

 save(df, file="data")

How I can load that saved data frame for future use? When I use:

  df2 <- load("data")

and I apply dim(df2) it should return the quantity of tweets that data frame has, but it only shows 1.

Answer

seancarmody picture seancarmody · Nov 3, 2012

As @mrdwab points out, save saves the names as well as the data/structure (and in fact can save a number of different R objects in a single file). There is another pair of storage functions that behave more as you expect. Try this:

saveRDS(df, file="mytweets.rds")
df2 <- readRDS("mytweets.rds")

These functions can only handle a single object at a time.