简体   繁体   English

在write.table中追加数据时使用列名

[英]using column names when appending data in write.table

I am looping through some data, and appending it to csv file. 我正在循环一些数据,并将其附加到csv文件。 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. 如果我执行col.names=T ,它会重复,包括每个新循环的列名。 If I have col.names=F , there are no column names at all. 如果我有col.names=F ,则根本没有列名。

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 . 请参阅?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. 您可能也可能不会看到行名称相同的问题,因为write.table在追加时不允许相同的行名称。 You could give this a try. 你可以尝试一下。 In the first write to file, try write.table with row.names = FALSE only. 在第一次写入文件,请尝试write.tablerow.names = FALSE只。 Then, starting from the second write to file, use both col.names = FALSE and row.names = FALSE 然后,从第二次写入到文件,使用col.names = FALSErow.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) . 我们可以用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) 我们可以再次使用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 ,那么将数据框附加到一起的代码块可能看起来像

> 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. 但正如@MrFlick建议的那样,将数据帧附加到R中然后将它们发送到文件一次会好得多。 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 . 尝试使用R中的names()命令更改数据框的列名,并使用与现有相同的名称替换,然后尝试使用dbWriteTable命令保持row.names = False The issue will get solved. 这个问题将得到解决。 eg 例如
if your data frame df1 has columns as obs , name , age then 如果你的数据框df1有列为obsnameage

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

and then try 然后试试

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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