Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: Print list to a text file

Tags:

list

r

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

like image 538
pms Avatar asked Jun 15 '10 11:06

pms


People also ask

How do I convert a list to a text file in R?

You can use the sink() function to quickly export a list to a CSV file or text file in R.

How do I write a list 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.


1 Answers

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

lapply(mylist, write, "test.txt", append=TRUE, ncolumns=1000) 
like image 184
nico Avatar answered Sep 30 '22 18:09

nico