R: Export and import a list to .txt file

亡梦爱人 提交于 2019-12-04 09:39:59

You can save your list using these commands (given that there are no element names containing a dot)

l1 <- list(a = 1, b = list(c = 1, d = 2))
vectorElements <- unlist(l1)
vectorPaths <- names(vectorElements)
vectorRows <- paste(vectorPaths, vectorElements)
write.table(vectorRows, "vectorRows.txt", row.names = FALSE, col.names = FALSE, quote = FALSE)

Each line of the file corresponds to an element in this format

node1.node2.node3 leaf

Then, you'll be able to re-build the list structure.

You could just use the yaml library:

R> library(yaml)
R> l1 <- list('1'=2, se7en=list('som:eth|ng~horr1ßl€'=42))
R> l1
$`1`
[1] 2

$se7en
$se7en$`som:eth|ng~horr1ßl€`
[1] 42
R> cat(as.yaml(l1), file='blah.txt')
R> l2 <- yaml.load_file('blah.txt')
R> l2
$`1`
[1] 2

$se7en
$se7en$`som:eth|ng~horr1ßl€`
[1] 42
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!