Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write col names while writing csv files in R

Tags:

r

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.

like image 815
motiur Avatar asked Feb 06 '23 08:02

motiur


1 Answers

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)
like image 169
Arun kumar mahesh Avatar answered Feb 10 '23 11:02

Arun kumar mahesh