[Rd] write.table does not quote col.names properly (PR#9044)

mwtoews at sfu.ca mwtoews at sfu.ca
Thu Jun 29 18:21:40 CEST 2006


Full_Name: Michael Toews
Version: 2.3.1
OS: Mac OS 10.4.6 and WinXP/SP2
Submission from: (NULL) (24.80.163.133)


This bug also affects related functions (write.csv, etc.), and can be
demonstrated using either a matrix or data frame:

m <- matrix(1:9,nrow=3,
       dimnames=list(c("A","B","C"),
                     c("I","II","III")))

write.table(m)
write.table(m,col.names=FALSE)
write.table(m,row.names=FALSE)

Note that the last 'write.table' call has unquoted column headers. A suggested
fix for this problem is:

Index: write.table.R
===================================================================
--- write.table.R       (revision 38447)
+++ write.table.R       (working copy)
@@ -86,9 +86,8 @@
     if(!is.null(col.names)) {
        if(append)
            warning("appending column names to file")
-       if(length(quote))
-           col.names <- paste("\"", gsub('"', qstring, col.names),
-                               "\"", sep = "")
+       col.names <- paste("\"", gsub('"', qstring, col.names),
+                           "\"", sep = "")
         writeLines(paste(col.names, collapse = sep), file, sep = eol)
     }
 

However, it is unknown why the "if(length(quote))" is used in the original code
(please check before a commit). The length of quote will be 0 here, since the
matrix has all numbers.
This bug does not occur with a data frame with mixed number and factor/character
columns.
+mt



More information about the R-devel mailing list