Riccardo
Riccardo

Reputation: 2017

Insert a blank row writing a file from a list using cat

using R, I have to write an output file from a list. So I think to use a 'for' loop to unlist and and write out.

l.spec <- list(a=c(rep("A",10)), b=c(rep("B",4)), c=c(rep("C",6)))
f <- matrix()
for(i in seq_along(l.spec)){
    f <- matrix(unlist(l.spec[i]), byrow=TRUE)
    cat(f, file="output.txt", sep="\n", append=TRUE)
}

Furthermor I need to insert a blank line between each level of the list to obtain an output such as:

A
A
A
A
A
A
A
A
A

B
B
B
B

C
C
C
C
C
C

Do you have any suggestion??

Regards

Riccardo

Upvotes: 2

Views: 1552

Answers (1)

Chase
Chase

Reputation: 69201

This seems to do what you want

lapply(l.spec, function(x) cat(c(x, " "), file = "output.txt", sep = "\n", append = TRUE))

Upvotes: 4

Related Questions