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.
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:These functions can only handle a single object at a time.
Another option is to save your data frame as a csv file. The benefit of this option is that it provides long term storage, i.e. you will (likely) be able to open your csv file on any platform in ten years time. With an
RData
file, you can only open it with R and I wouldn't like to bet money on opening it between versions.To save the file as a csv, just use:
read.csv
andwrite.csv
, so:Gavin's comment below raised a couple of points:
Completely correct. But if you are saving a data frame (as the OP is), then your data is in tabular form.
To play devil's adovacate, you could use this argument with Excel and save your data as an
xls
. However, saving your data in a csv format means we never need to worry about this.I completely agree - although "easily" is a bit strong. This is why saving as an RData file isn't such a big deal. But if you are saving tabular data, why not use a csv file?
For the record, there are some reasons for saving tabular data as an RData file. For example, the speed in reading/writing the file or file size.
save
saves the name of the dataset as well as the data. Thus, you should not not assign a name toload("data")
and you should be fine. In other words, simply use:and it will load an object named
df
(or whatever is contained in the file "data") into your current workspace.I would suggest a more original name for your file though, and consider adding an extension to help you remember what your script files are, your data files are, and so on.
Work your way through this simple example:
As you can see,
save
allows you to save and load multiple objects at once, which can be convenient when working on projects with multiple sets of data that you want to keep together.On the other hand,
saveRDS
(from the accepted answer) only lets you save single objects. In some ways, this is more "transparent" sinceload()
doesn't let you preview the contents of the file without first loading it.