How to save a data.frame in R?

RDataframe

R Problem Overview


I made a data.frame in R that is not very big, but it takes quite some time to build. I would to save it as a file, which I can than again open in R?

R Solutions


Solution 1 - R

There are several ways. One way is to use save() to save the exact object. e.g. for data frame foo:

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

Then load it with:

load("data.Rda")

You could also use write.table() or something like that to save the table in plain text, or dput() to obtain R code to reproduce the table.

Solution 2 - R

If you are only saving a single object (your data frame), you could also use saveRDS.
To save:

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

Then read it with:

bar <- readRDS(file="data.Rda")

The difference between saveRDS and save is that in the former only one object can be saved and the name of the object is not forced to be the same after you load it.

Solution 3 - R

If you have a data frame named df, you can simply export it to same directory with:

write.csv(df, "output.csv", row.names=FALSE, quote=FALSE) 

credit to: Peter and Ilja, UMCG, the Netherlands.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBorut FlisView Question on Stackoverflow
Solution 1 - RSacha EpskampView Answer on Stackoverflow
Solution 2 - RdhendricksonView Answer on Stackoverflow
Solution 3 - RNigus AsefaView Answer on Stackoverflow