Writing a list of vectors to a file

Suppose you have a list whose components are vectors of different length, such as:

a <- list(comp1 = 1:2, comp2 = 3:6, comp3 = 7:9)

Here's how you can write the list to a file such that each component occupies a row of the output file --- therefore, the output file has a different number of columns for each row.

cat(sapply(a, paste, collapse = "\t"), sep = "\n", file = "filename.txt")
In the previous code, we first collapse the elements of each vector to be tab delited ("\t"), and then we write each vector to the file as separate rows ("\n").
Topic revision: r1 - 21 Nov 2006, TheresaScott
 

This site is powered by FoswikiCopyright © 2013-2017 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding Vanderbilt Biostatistics Wiki? Send feedback