[R] header line generated write.table
MSchwartz at MedAnalytics.com
Thu Aug 19 00:09:14 CEST 2004
On Wed, 2004-08-18 at 16:42, Y C Tao wrote:
> I want to write following data frame into a CSV file:
> Col1 Col2 Col3
> Row1 1 1 1
> Row2 2 2 2
> where Row1, Row2 are the row names and Col1, Col2, Col3 are the column
> The correct CSV file should be:
> However, the one generated by R using write.table(x, file="xyz.csv",
> sep=",") has a header line that reads:
> without the comma at the very beginning.
> As a result, if you open the file in Excel, the column names are not
> correct (shifted to the left by one column).
> Is there a way to get around this?
The solution is on the help page for ?write.table:
Normally there is no column name for a column of row names. If
col.names=NA a blank column name is added. This can be used to write CSV
files for input to spreadsheets.
Also, the first example on that page gives you:
## To write a CSV file for input to Excel one might use
write.table(x, file = "foo.csv", sep = ",", col.names = NA)
> write.table(x, col.names = NA, sep = ",")
More information about the R-help