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