From j|ox @end|ng |rom mcm@@ter@c@  Fri Jul  2 00:47:09 2021
From: j|ox @end|ng |rom mcm@@ter@c@ (John Fox)
Date: Thu, 1 Jul 2021 18:47:09 -0400
Subject: [Rd] On read.csv and write.csv
In-Reply-To: <23268_1625178608_161MU7RV006249_CAD4oTHFjYycJzEtLMM3jJ8ZbZjUCY6-Vh_-zUvHin3L15iXS1g@mail.gmail.com>
References: <CWXP265MB3208A3DC3EE8F8E8489E258B81019@CWXP265MB3208.GBRP265.PROD.OUTLOOK.COM>
 <029F6720-64AB-4F19-A823-8DB0768E153F@R-project.org>
 <CWXP265MB32085A57128A1822E4A5C5BB81009@CWXP265MB3208.GBRP265.PROD.OUTLOOK.COM>
 <23268_1625178608_161MU7RV006249_CAD4oTHFjYycJzEtLMM3jJ8ZbZjUCY6-Vh_-zUvHin3L15iXS1g@mail.gmail.com>
Message-ID: <b913f9ae-66d3-bd33-791f-9d971b2a1cfc@mcmaster.ca>

Dear Gabriel,

On 2021-07-01 6:29 p.m., Gabriel Becker wrote:
> On Thu, Jul 1, 2021 at 1:46 PM Stephen Ellison <S.Ellison at lgcgroup.com>
> wrote:
> 
>>
>> 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.
>>
> 
> This depends on if you are reading the csv via R or something else, I would
> imagine. It not being "valid" CSV at all would likely cause some programs
> to choke entirely, I expect. I admit that's conjecture though, I don't have
> data on that one way or another.

On Excel, for example, opening a .csv file without the empty initial 
field in the first line will cause the column names to be misaligned.

As others have pointed out, .csv files are meant as a sort of 
least-common-denominator of data exchange, and so following the standard 
is probably a good idea.

Best,
  John

John Fox, Professor Emeritus
McMaster University
Hamilton, Ontario, Canada
web: https://socialsciences.mcmaster.ca/jfox/

> 
> ~G
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>



[Rd] On read.csv and write.csv
John Fox