R: Print list to a text file

ListR

List Problem Overview


I have in R a list like this:

> 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().

List Solutions


Solution 1 - List

Not tested, but it should work (edited after comments)

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

Solution 2 - List

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.

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.

sink("mylist.txt")
print(mylist)
sink()

Make sure to have the sink() at the end your code so that you cancel the sink.

Solution 3 - List

Another way

writeLines(unlist(lapply(mylist, paste, collapse=" ")))

Solution 4 - List

Here's another way using sink:

sink(sink_dir_and_file_name); print(yourList); sink()

Solution 5 - List

depending on your tastes, an alternative to nico's answer:

d<-lapply(mylist, write, file=" ... ", append=T);

Solution 6 - List

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.

dput(mylist, "mylist.txt")

Solution 7 - List

Here is another

cat(sapply(mylist, toString), file, sep="\n")

Solution 8 - List

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionpmsView Question on Stackoverflow
Solution 1 - ListnicoView Answer on Stackoverflow
Solution 2 - ListJasonView Answer on Stackoverflow
Solution 3 - ListMarekView Answer on Stackoverflow
Solution 4 - Listuser6585653View Answer on Stackoverflow
Solution 5 - ListCarlView Answer on Stackoverflow
Solution 6 - ListDanny Boy150View Answer on Stackoverflow
Solution 7 - Listuser6137425View Answer on Stackoverflow
Solution 8 - ListkujungmulView Answer on Stackoverflow