4
votes

What is the proper way to append col names to the header of csv table which is generated by write.table command? For example write.table(x, file, col.names= c("ABC","ERF")) throws error saying invalid col.names specification.Is there way to get around the error, while maintaining the function header of write.table.

Edit: I am in the middle of writing large code, so exact data replication is not possible - however, this is what I have done: write.table(paste("A","B"), file="AB.csv", col.names=c("A1","B1")) , I am still getting this error Error in write.table(paste("A","B"), file="AB.csv", col.names=c("A", : invalid 'col.names' specification.

1
According to the docs, that should work. Unless you don't have exactly 2 columns. Please provide a reproducible example.Rich Scriven
@motiur When i use your code there is no problem in writing column names if i have 2 columns in a dataArun kumar mahesh
Please see my edits.motiur
paste("A", "B") returns a single string/column, and you specify two column names. Hence, the error.Rich Scriven
I want to concatenate two variables - so I thought this would be a good option - should I use c("A","B")motiur

1 Answers

9
votes

Is that what you expect, tried my end

df <- data.frame(condition_1sec=1)

df1 <- data.frame(susp=0)

write.table(c(df,df1),file="table.csv",col.names = c("A","B"),sep = ",",row.names = F)