R:将列表导出并导入到.txt文件 [英] R: Export and import a list to .txt file

查看:1190
本文介绍了R:将列表导出并导入到.txt文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这篇文章提出了一种将列表写入文件的方法文件.

This post suggests a way to write a list to a file.

lapply(mylist, write, "test.txt", append=TRUE, ncolumns=1000)

该技术的问题在于,列表信息的一部分(分为子部分的结构和子部分的名称)消失了,因此重新创建原始信息非常复杂(如果丢失了额外的信息则是不可能的)从文件中列出.

The issue with this technic is that part of the information of the list (the structure into subparts and the names of the subparts) disappear and it is therefore very complicated (or impossible if we lost the extra information) to recreate the original list from the file.

什么是导出和导入(不进行任何修改,包括名称在内)列表的最佳解决方案?

推荐答案

您可以使用以下命令保存列表(假设没有包含点的元素名称)

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.

这篇关于R:将列表导出并导入到.txt文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆