Neal H. Walfield
2014-Oct-08 14:22 UTC
[R] write.table produces a file that read.table can't read
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
MacQueen, Don
2014-Oct-08 16:34 UTC
[R] write.table produces a file that read.table can't read
How about: tmp <- data.frame(a=1:3, b=c("a", "b\"b", "c")) write.table(tmp, './tmp.write', row.names=FALSE, sep="\t?, quote=FALSE) tmp.in <- read.table('./tmp.write', sep='\t', head=TRUE, quote="")> all.equal(tmp, tmp.in)[1] TRUE -Don -- Don MacQueen Lawrence Livermore National Laboratory 7000 East Ave., L-627 Livermore, CA 94550 925-423-1062 On 10/8/14, 7:22 AM, "Neal H. Walfield" <neal at walfield.org> wrote:>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 > >______________________________________________ >R-help at r-project.org mailing list >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide >http://www.R-project.org/posting-guide.html >and provide commented, minimal, self-contained, reproducible code.