pms pms - 2 months ago 7
R Question

R: Print list to a text file

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

Answer

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

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