list R:将列表打印到文本文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3044794/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
R: Print list to a text file
提问by pms
I have in R a list like this:
我在 R 中有一个这样的列表:
> print(head(mylist,2))
[[1]]
[1] 234984 10354 41175 932711 426928
[[2]]
[1] 1693237 13462
Each element of the list has different number of its elements.
列表的每个元素都有不同数量的元素。
I would like to print this list to a text file like this:
我想将此列表打印到这样的文本文件中:
mylist.txt
234984 10354 41175 932711 426928
1693237 13462
I know that I can use sink(), but it prints names of elements [[x]], [y] and I want to avoid it. Also because of different number of elements in each element of the list it is not possible to use write() or write.table().
我知道我可以使用 sink(),但它会打印元素 [[x]]、[y] 的名称,我想避免使用它。此外,由于列表中每个元素的元素数量不同,因此无法使用 write() 或 write.table()。
回答by nico
Not tested, but it shouldwork (edited after comments)
未经测试,但它应该可以工作(评论后编辑)
lapply(mylist, write, "test.txt", append=TRUE, ncolumns=1000)
回答by Jason
I saw in the comments for Nico's answer that some people were running into issues with saving lists that had lists within them. I also ran into this problem with some of my work and was hoping that someone found a better answer than what I found however no one responded to their issue.
我在 Nico 的回答的评论中看到,有些人在保存包含列表的列表时遇到了问题。我在我的一些工作中也遇到了这个问题,并希望有人找到比我发现的更好的答案,但是没有人回应他们的问题。
So: @ali, @FMKerckhof, and @Kerry the only way that I found to save a nested list is to use sink() like user6585653 suggested (I tried to up vote his answer but could not). It is not the best way to do it since you link the text file which means it can be easily over written or other results may be saved within that file if you do not cancel the sink. See below for the code.
所以:@ali、@FMKerckhof 和 @Kerry 我发现保存嵌套列表的唯一方法是像 user6585653 建议的那样使用 sink()(我试图对他的答案投赞成票,但不能)。这不是最好的方法,因为您链接了文本文件,这意味着它可以很容易地被覆盖,或者如果您不取消接收器,其他结果可能会保存在该文件中。请参阅下面的代码。
sink("mylist.txt")
print(mylist)
sink()
Make sure to have the sink() at the end your code so that you cancel the sink.
确保在代码末尾使用 sink(),以便取消 sink。
回答by Marek
Another way
其它的办法
writeLines(unlist(lapply(mylist, paste, collapse=" ")))
回答by user6585653
Here's another way using sink:
这是使用 sink 的另一种方法:
sink(sink_dir_and_file_name); print(yourList); sink()
sink(sink_dir_and_file_name); print(yourList); sink()
回答by Carl
depending on your tastes, an alternative to nico's answer:
根据您的口味,替代nico的答案:
d<-lapply(mylist, write, file=" ... ", append=T);
回答by user6137425
Here is another
这是另一个
cat(sapply(mylist, toString), file, sep="\n")
回答by Danny Boy150
Format won't be completely the same, but it does write the data to a text file, and R will be able to reread it using dget
when you want to retrieve it again as a list.
格式不会完全相同,但它确实将数据写入文本文件,dget
当您想将其作为列表再次检索时,R 将能够重新读取它。
dput(mylist, "mylist.txt")
回答by kujungmul
I solve this problem by mixing the solutions above.
我通过混合上述解决方案来解决这个问题。
sink("/Users/my/myTest.dat")
writeLines(unlist(lapply(k, paste, collapse=" ")))
sink()
I think it works well
我认为它运作良好