[R] write.table produces a file that read.table can't read

Neal H. Walfield neal at walfield.org
Wed Oct 8 16:22:13 CEST 2014


Hi,

I'm using R version 3.1.1 on Debian via the CRAN repositories.

Consider the following MWE that writes a data.frame out to a file and
reads it back in (note that one of the strings contains a double
quote):

  > write.table(data.frame(a=1:3, b=c("a", "b\"b", "c")), '/tmp/a', row.names=FALSE, na="?", sep=",")
  > read.table('/tmp/a', header=TRUE, row.names=NULL, sep=',', na.strings='?', allowEscapes=T)
  [1] a b
  <0 rows> (or 0-length row.names)

/tmp/a contains the following:

  $ cat /tmp/a 
  "a","b"
  1,"a"
  2,"b\"b"
  3,"c"

Removing the double quote, it works:

  > write.table(data.frame(a=1:3, b=c("a", "bb", "c")), '/tmp/a', row.names=FALSE, na="?", sep=",")
  > read.table('/tmp/a', header=TRUE, row.names=NULL, sep=',', na.strings='?', allowEscapes=T)
    a  b
  1 1  a
  2 2 bb
  3 3  c

Why does allowEscapes not work for double quotes?  Or, why does
write.table produce a file that read.table can't read!

Thanks for any advice!

Neal



More information about the R-help mailing list