简体   繁体   中英

using column names when appending data in write.table

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 col.names=T , it repeats including column names for each new loop. If I have col.names=F , 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)

See ?file.exists .

write.table(dd, "data.csv", append=TRUE, col.names=!file.exists("data.csv"))

Thus column names are written only when you are not appending to a file that already exists.

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 <- do.call(rbind, dlst)
> write.table(dc, "file.txt")

Try changing the column names of the data frame using names() command in R and replace with the same names as existing and then try the dbWriteTable command keeping row.names = False . The issue will get solved. eg
if your data frame df1 has columns as obs , name , age then

names(df1) <- c('obs','name','age')

and then try

dbWriteTable(conn, 'table_name', df1, append = T, row.names = F) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM