user1754606 user1754606 - 1 year ago 80
R Question

Write column header once only, when writing data with write.table(append=T)

I am looping through some data, and appending it to csv file. What I want is to have column names on the top of the file once, and then as it loops to not repeat column names in the middle of file.

If I do

, it repeats including column names for each new loop. If I have
, there are no column names at all.

How do I do this most efficiently? I feel that this is such a common case that there must be a way to do it, without writing code especially to handle it.

write.table(dd, "data.csv", append=TRUE, col.names=T)

Answer Source

You may or may not also see a problem with the row names being identical, as write.table does not allow identical row names when appending. You could give this a try. In the first write to file, try write.table with row.names = FALSE only. Then, starting from the second write to file, use both col.names = FALSE and row.names = FALSE

Here's the first write to file

> d1 <- data.frame(A = 1:5, B = 1:5)                ## example data
> write.table(d1, "file.txt", row.names = FALSE)

We can check it with read.table("file.txt", header = TRUE). Then we can append the same data frame to that file with

> write.table(d1, "file.txt", row.names = FALSE, 
              col.names = FALSE, append = TRUE)

And again we can check it with read.table("file.txt", header = TRUE)

So, if you have a list of data frames, say dlst, your code chunk that appends the data frames together might look something like

> dlst <- rep(list(d1), 3)                              ## list of example data
> write.table(dlst[1], "file.txt", row.names = FALSE)  
> invisible(lapply(dlst[-1], write.table, "file.txt", row.names = FALSE, 
                   col.names = FALSE, append = TRUE))

But as @MrFlick suggests, it would be much better to append the data frames in R, and then send them to file once. This would eliminate many possible errors/problems that could occur while writing to file. If the data is in a list, that could be done with

> dc <-, dlst)
> write.table(dc, "file.txt")
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download