简体   繁体   English

如何在 R 中保存 data.frame?

[英]How to save a data.frame in R?

I made a data.frame in R that is not very big, but it takes quite some time to build.我在 R 中创建了一个不是很大的 data.frame,但是构建起来需要相当长的时间。 I would to save it as a file, which I can than again open in R?我想将它保存为一个文件,然后我可以再次在 R 中打开它?

There are several ways.有几种方法。 One way is to use save() to save the exact object.一种方法是使用save()来保存确切的对象。 eg for data frame foo :例如对于数据框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.您还可以使用write.table()或类似的东西以纯文本格式保存表格,或使用dput()获取 R 代码来重现表格。

If you are only saving a single object (your data frame), you could also use saveRDS .如果您只保存单个对象(您的数据框),您还可以使用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. saveRDSsave的区别在于前者只能保存一个对象,并且加载后对象的名称不会强制相同。

If you have a data frame named df , you can simply export it to same directory with:如果您有一个名为df的数据框,您可以简单地将其导出到同一目录:

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

credit to: Peter and Ilja, UMCG, the Netherlands.归功于:荷兰 UMCG 的 Peter 和 Ilja。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM