[R] header line generated write.table
Marc Schwartz
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
> names.
>
> The correct CSV file should be:
> ,"Col1","Col2","Col3"
> Row1,1,1,1
> Row2,2,2,2
>
> However, the one generated by R using write.table(x, file="xyz.csv",
> sep=",") has a header line that reads:
> "Col1","Col2","Col3"
> 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?
>
> Thanks!
The solution is on the help page for ?write.table:
Details
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)
Thus:
> write.table(x, col.names = NA, sep = ",")
"","Col1","Col2","Col3"
"Row1",1,1,1
"Row2",2,2,2
HTH,
Marc Schwartz
More information about the R-help
mailing list