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().
You can use the sink() function to quickly export a list to a CSV file or text file in R.
How to Create Lists in R? We can use the list() function to create a list. Another way to create a list is to use the c() function. The c() function coerces elements into the same type, so, if there is a list amongst the elements, then all elements are turned into components of a list.
Not tested, but it should work (edited after comments)
lapply(mylist, write, "test.txt", append=TRUE, ncolumns=1000)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With