[R] adding header info to write.table

Spencer Graves spencer.graves at pdf.com
Thu Feb 26 22:55:59 CET 2004

      A minor variant is to use "cat" for the initial headers: 

DF <- data.frame(a=1:2, b=c("a","B"))
cat("header1\nheader2\n", file="filename.csv")
cat("header3\n", file="filename.csv", append=TRUE)
write.table(DF, file="filename.csv", append=TRUE)

      In S-Plus 6.2 and R 1.8.2, this produces the following: 

"a" "b"
"1" 1 "a"
"2" 2 "B"

      hope this helps.  spencer graves

Liaw, Andy wrote:

>I suppose one way to do it is to write the header info to the file first
>(e.g., through connections, or sink(), etc.), then do write.table(...,
>>From: femke
>>Could someone please tell if there is a way to append header 
>>info to the top of an exported dataframe (exported with 
>>write.table).  I want to append the following, which are the 
>>defininitions for an asciigrid:
>>ncols         532
>>nrows         999
>>xllcorner     510465
>>yllcorner     4766375
>>cellsize      30
>>NODATA_value  -9999
>>Thanks very much,
>>	[[alternative HTML version deleted]]
>>R-help at stat.math.ethz.ch mailing list
>>PLEASE do read the posting guide! 
>Notice:  This e-mail message, together with any attachments,...{{dropped}}
>R-help at stat.math.ethz.ch mailing list
>PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html

More information about the R-help mailing list