[R] Writing out csv files

James Rome jamesrome at gmail.com
Tue Feb 2 21:32:28 CET 2010


In my code, I calculate the maximum values with 2 factors using
maxr=with(arrdf, tapply(rate,list(weekday,quarter), max, na.rm=T))

and I want to write out the file so that Excel can read it.
I used
write.table(maxr, fname, sep=",", col.names=TRUE, row.names=TRUE,
                quote=TRUE, na="0")
which works, and yields something like
"0","1","2","3","4","5","6","7","8","9","10","11","12","13","14"
"Friday",4,3,2,2,2,2,1,2,2,2,1,2,1,1,1,1,1,2,3,4,5,6,9,8,8,5,8,1
"Monday",3,2,1,2,1,2,1,1,1,2,1,0,0,2,2,1,2,2,4,4,4,7,7,7,7,6,8,1
"Saturday",4,3,1,1,3,2,1,1,1,2,1,0,0,2,0,1,1,2,5,6,4,5,5,8,8,6,6
"Sunday",3,2,4,1,2,2,1,1,2,2,1,0,1,1,1,1,0,1,4,3,5,6,5,8,6,5,5,1
"Thursday",3,3,2,3,2,1,0,1,3,1,0,0,1,1,0,0,2,3,4,4,5,5,5,8,6,4,6
"Tuesday",5,3,2,2,2,3,1,1,0,0,1,0,1,1,1,1,1,2,3,4,7,5,8,8,7,7,7,
"Wednesday",2,3,1,2,2,1,1,1,1,1,1,0,0,0,2,1,1,3,4,6,4,6,11,10,5,

The top row is the quarter hours of the day

However, when Excel reads this, it will put the first "0" over Friday
instead of over the 4.

Is there a way to write this out with a "quarter" at the start of the
first row? E.g.,

"quarter", "0","1","2","3","4","5","6","7","8","9","10","11","12","13","14"
"Friday",4,3,2,2,2,2,1,2,2,2,1,2,1,1,1,1,1,2,3,4,5,6,9,8,8,5,8,1
"Monday",3,2,1,2,1,2,1,1,1,2,1,0,0,2,2,1,2,2,4,4,4,7,7,7,7,6,8,1
"Saturday",4,3,1,1,3,2,1,1,1,2,1,0,0,2,0,1,1,2,5,6,4,5,5,8,8,6,6
"Sunday",3,2,4,1,2,2,1,1,2,2,1,0,1,1,1,1,0,1,4,3,5,6,5,8,6,5,5,1
"Thursday",3,3,2,3,2,1,0,1,3,1,0,0,1,1,0,0,2,3,4,4,5,5,5,8,6,4,6
"Tuesday",5,3,2,2,2,3,1,1,0,0,1,0,1,1,1,1,1,2,3,4,7,5,8,8,7,7,7,
"Wednesday",2,3,1,2,2,1,1,1,1,1,1,0,0,0,2,1,1,3,4,6,4,6,11,10,5,

And I need to keep the non-standard column names too.

Thanks again in advance,
Jim Rome



More information about the R-help mailing list