MichaelChirico / r-bugs

A ⚠️read-only⚠️mirror of https://bugs.r-project.org/
20 stars 0 forks source link

[BUGZILLA #15951] when including row names, write.csv and write.table write inconsistent column name lines. #5427

Open MichaelChirico opened 4 years ago

MichaelChirico commented 4 years ago

When including row names, write.csv writes the column name line prefixed with a comma to indicate that row names are present. However, write.table with sep=',' does not output this initial comma. I'd guess that most folks would expect these two calls to produce the same output. The behavior of write.csv seems the most appropriate.

Example

sample.data <- data.frame(a=1:5, b=letters[1:5]) temp1 <- tempfile() write.csv(sample.data, temp1, quote=FALSE) readChar(temp1, file.info(temp1)$size) # note the first character is a comma

write.table(sample.data, temp1, sep=',', quote=FALSE) readChar(temp1, file.info(temp1)$size) # note that the initial comma from the previous example isn't present

unlink(temp1)


METADATA

MichaelChirico commented 4 years ago

The help page talks about this; indeed, write.csv is behaving as documented...

CSV files:

 By default there is no column name for a column of row names.  If
 ‘col.names = NA’ and ‘row.names = TRUE’ a blank column name is
 added, which is the convention used for CSV files to be read by
 spreadsheets.

    <snip>

 There is an IETF RFC4180 (<URL:
 https://tools.ietf.org/html/rfc4180>) for CSV files, which
 mandates comma as the separator and CRLF line endings.
 ‘write.csv’ writes compliant files on Windows: use ‘eol = "\r\n"’
 on other platforms.

So if you want write.table to mimic write.csv, then supply 'col.names = NA',

write.table(sample.data, temp1, sep=',', quote=FALSE, col.names = NA)
readLines(temp1)

[1] ",a,b" "1,1,a" "2,2,b" "3,3,c" "4,4,d" "5,5,e"


METADATA