[Rd] On read.csv and write.csv

Stephen Ellison S@E|||@on @end|ng |rom LGCGroup@com
Wed Jun 30 23:15:03 CEST 2021


Apologies if this is a well-worn question; I haven’t found it so far but there's a lot of r-dev and I may have missed it in the archives. In the mean time:

I've managed to avoid writing csv files with R for a couple of decades but we're swopping data with a collaborator and I've tripped over an inconsistency between read.csv and write.csv that seems less than helpful.
The default line number behaviour for read.csv is to assume that, when the number of items in the first row is one less than the number in the second, that the first column contains row names. write.csv, however, includes an empty string ("") as the first header entry over row names when writing. On rereading, the original row names are then treated as data with unknown name, replaced by "X".

That means that, unlike read.table and write.table,  something written with write.csv is not read back correctly by read.csv .

Is that intentional?
And whether it is intentional or not, is it wise?

Example:

( D1 <- data.frame(A=letters[1:5], N=1:5, Y=rnorm(5) ) )
write.csv(D1, "temp.csv")

( D1w <- read.csv("temp.csv") )

# Note the unnecessary new X column ...
#Tidy up
unlink("temp.csv")

This differs from the parent .table defaults; write.table doesn’t add the extra "" column label, so the object read back with read.table does not contain an unwanted extra column.

Wouldn’t it be more sensible if write.csv() and read.csv() were consistent in the same sense as read.table and write.table?
Or at least if there were a switch (as.read.csv=TRUE ?) to tell write.csv to omit the initial "", or vice versa?

Currently using R version 4.1.0 on Windows, but this reproduces at least as far back as 3.6 

Steve E


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