How can i save a list to a file and read it in again (in R)?

試著忘記壹切 提交于 2019-12-04 23:38:35
save(starting.values, file="fname.RData")
?save

Allows you to save one or more R objects to a single file.

Load with

load("fname.RData")

I would also suggest that you explore rlist package and the offered list.save function as it provides additional flexibility when saving lists.

Example

As available in documentation.

require(rlist)
x <- lapply(1:5,function(i) data.frame(a=i,b=i^2))
list.save(x, 'list.rds')
list.save(x, 'list.rdata')
list.save(x, 'list.yaml')

yaml file preview

- a: 1
  b: 1.0
- a: 2
  b: 4.0
- a: 3
  b: 9.0
- a: 4
  b: 16.0
- a: 5
  b: 25.0

You could then load list with use of the list.load function in the same package or you could read lines from the exported yaml file. You could also consider having a look at the list.select function and selecting objects from within list. Having said that, I presume that the easiest way to approach this would be simply to store/load a full list object.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!