Stephen,

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.

Cheers,
Simon

PS: this is likely a question for R-help rather than R-devel



> On 1/07/2021, at 9:15 AM, Stephen Ellison <s.elli...@lgcgroup.com> wrote:
> 
> 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 u...{{dropped:13}}

______________________________________________
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel

Reply via email to