R: Write ASCII file where column names (header) is separated differently than all other rows
I'm struggling with an export of a data frame to an ASCII file. I basically want it to look like this:
<Date> <Stock> <Price> 19900101, SOMESTOCK, 100.00 19900102, SOMESTOCK, 101.00
How would I do that? What I have so far is:
write.table(MyDataFrame, file=File, row.names=FALSE, col.names=TRUE, sep=",")
If you want that very exact output (i.e. column names between less-than and greather-than signs, separated by spaces and content separated by commas), then:
a <- data.frame(Date=1:2,Stock=letters[1:2],Price=runif(2)) #A test case file <- "a.txt" cat(paste("<",colnames(a),">",sep=""),"\n",file=file) write.table(a, file=file, row.names=FALSE, col.names=FALSE, sep=", ", append=TRUE, quote=FALSE)
Note the use of argument append=TRUE in write.table and the use of argument file in cat.