Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read \" double-quote escaped values with read.table in R

I am having trouble to read a file containing lines like the one below in R.

"_:b5507F4C7x59005","Fabiana D\"atri"

Any idea? How can I make read.table understand that \" is the escape of quote?

Cheers, Alexandre

like image 518
Alexandre Rademaker Avatar asked Aug 15 '11 15:08

Alexandre Rademaker


2 Answers

I was able to get your eample to work by setting the quote argument:

> read.csv('test.csv',quote="'",head=FALSE)
                   V1                  V2
1 "_:b5507F4C7x59005" "Fabiana D\\"atri" 
2 "_:b5507F4C7x59005" "Fabiana D\\"atri" 
like image 117
nullglob Avatar answered Nov 07 '22 19:11

nullglob


My apologies ahead of time that this isn't more detailed -- I'm right in the middle of a code crunch.

You might consider using the scan() function. I created a simple sample file "sample.csv," which consists of:

V1,V2
"_:b5507F4C7x59005","Fabiana D\"atri"

Two quick possibilities are (with output commented so you can copy-paste to the command line):

test <- scan("sample.csv", sep=",", what='character',allowEscapes=TRUE)
## Read 4 items
test
##[1] "V1"                "V2"                "_:b5507F4C7x59005"
##[4] "Fabiana D\\atri\n"

or

test <- scan("sample.csv", sep=",", what='character',comment.char="\\")
## Read 4 items
test
## [1] "V1"                "V2"                "_:b5507F4C7x59005"
## [4] "Fabiana D\\atri\n"

You'll probably need to play around with it a little more to get what you want. And I see that you've already mentioned writeLines, so you may have already tried this. Either way, good luck!

like image 45
CompEcon Avatar answered Nov 07 '22 20:11

CompEcon