Open MichaelChirico opened 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"
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
withsep=','
does not output this initial comma. I'd guess that most folks would expect these two calls to produce the same output. The behavior ofwrite.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