How to save a data.frame in R?

后端 未结 3 2095
执笔经年
执笔经年 2020-11-28 19:05

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?

相关标签:
3条回答
  • 2020-11-28 19:45

    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.

    0 讨论(0)
  • 2020-11-28 20:01

    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.

    0 讨论(0)
  • 2020-11-28 20:05

    Let us say you have a data frame you created and named "Data_output", you can simply export it to same directory by using the following syntax.

    write.csv(Data_output, "output.csv", row.names = F, quote = F)

    credit to Peter and Ilja, UMCG, the Netherlands

    0 讨论(0)
提交回复
热议问题