[R] Using read.table to read file created with read.table and qmethod = "escape"

David Winsemius dwinsemius at comcast.net
Thu Apr 8 15:43:36 CEST 2010


On Apr 8, 2010, at 9:20 AM, jim holtman wrote:

> You were using read.csv and not read.table.  The following seems to  
> work
> with using a separator that will probably not appear in the text:

Modified  Jim's version:
>
>> df <- data.frame(a = "a\"b", v = 4, z = "this is: A, B, C",  
>> stringsAsFactors=FALSE)
>> write.table(df, "test.csv", row.names = FALSE, quote = FALSE,  
>> sep='\x01')
>> df2 <-read.table("test.csv", quote = "", sep='\x01', header=TRUE,  
>> stringsAsFactors=FALSE)
>    a v                z
> 1 a"b 4 this is: A, B, C
>>

It also works if you create and read them as character vectors  
with  ... , stringsAsFactors=FALSE) although the '"'  now gets  
displayed at the console with an escape since it is not a level label.

 > df2$a
[1] "a\"b"

-- 
David.

>
> On Thu, Apr 8, 2010 at 9:07 AM, Hadley Wickham <hadley at rice.edu>  
> wrote:
>
>>> df <- data.frame(a = "a\"b", v = 4, z = "this is Z")
>>> write.csv(df, "test.csv", row.names = FALSE, quote = FALSE)
>>> read.csv("test.csv", quote = "")
>>
>> Unfortunately my real example is more like:
>>
>> df <- data.frame(a = "a\"b", v = 4, z = "this is: A, B, C")
>>
>> so quote = F won't work.
>>
>> Can write.table and read.table really be so asymmetric?
>>
>> Hadley
>
>
> -- 
> Jim Holtman
> Cincinnati, OH
> +1 513 646 9390
>
> What is the problem that you are trying to solve?

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list