[Rd] On read.csv and write.csv

Stephen Ellison S@E|||@on @end|ng |rom LGCGroup@com
Thu Jul 1 22:46:13 CEST 2021


> the "unhelpful" column are the row names. They are considered an
> important part of a data frame and therefore the default (row.names =
> TRUE) is to not lose them (as there is no way back once you do). If you don't
> want to preserve the row names you can simply set row.names=FALSE.

Please run the reproducible example provided. 
When you do, you will see that write.csv writes an unnecessary empty header field ("") over the row names column. This makes the number of header fields equal to the number of columns _including_ row names. That causes the original row names to be read as data by read.csv, following the rule that the number of header fields determines whether row names are present. read.csv  accordingly assumes that the former row names are unnamed data, calls the unnamed row names column "X" (or X.1 etc if X exists) and then adds new, default, row names _instead of the original row names written by write.csv_. 
That's not helpful.

By contrast read.table correctly reads the first entry in each row as a row name when the number of header fields is one less than the number of data columns. write.table includes row names as row names _without a header field_, so a file written with write.table is correctly formatted for read.table to interpret the first data field as a row name.
I think it would be more sensible if write.csv did the same as write.table when row.names=TRUE - as it is, by default.





*******************************************************************
This email and any attachments are confidential. Any use, copying or
disclosure other than by the intended recipient is unauthorised. If 
you have received this message in error, please notify the sender 
immediately via +44(0)20 8943 7000 or notify postmaster using lgcgroup.com 
and delete this message and any copies from your computer and network. 
LGC Limited. Registered in England 2991879. 
Registered office: Queens Road, Teddington, Middlesex, TW11 0LY, UK


More information about the R-devel mailing list