Skip to content
Prev 87227 / 398506 Next

Adding header lines to a dataframe that is exported using write.csv

On 2/25/2006 1:20 PM, Mark wrote:
Open a connection and write the header to it first, then write the
dataframe.  For example,

df <- data.frame(a=1:5,b=6:10)

f <- file("dataframe.csv", "w")

writeLines(paste(c(nrow(df), ncol(df)), c("plots", "species"), ",,,", 
sep=","),f)
writeLines(paste(rep(",Q", ncol(df)), collapse=""),f)
write.csv(df, f)

close(f)

Duncan Murdoch