From S@E|||@on @end|ng |rom LGCGroup@com  Thu Jul  1 22:46:13 2021
From: S@E|||@on @end|ng |rom LGCGroup@com (Stephen Ellison)
Date: Thu, 1 Jul 2021 20:46:13 +0000
Subject: [Rd] On read.csv and write.csv
In-Reply-To: <029F6720-64AB-4F19-A823-8DB0768E153F@R-project.org>
References: <CWXP265MB3208A3DC3EE8F8E8489E258B81019@CWXP265MB3208.GBRP265.PROD.OUTLOOK.COM>
 <029F6720-64AB-4F19-A823-8DB0768E153F@R-project.org>
Message-ID: <CWXP265MB32085A57128A1822E4A5C5BB81009@CWXP265MB3208.GBRP265.PROD.OUTLOOK.COM>

> 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 at 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


[Rd] On read.csv and write.csv
Stephen Ellison