[R] read.csv quotes within fields

Duncan Murdoch murdoch.duncan at gmail.com
Sat Jan 26 00:36:59 CET 2013

On 13-01-25 4:37 PM, Tim Howard wrote:
> David,
> Thank you again for the reply. I'll try to make readLines() and strplit() work.  What bugs me is that I think it would import fine if the folks who created the csv had used double quotes "" rather than an escaped quote \" for those pesky internal quotes. Since that's the case, I'd think there would be a solution within read.csv() ... or perhaps scan()?, I just can't figure it out.

What you say doesn't make sense.  Let me paraphrase:

"The folks who sent me the data created a weird, non-standard .csv file. 
  You'd think read.csv() could handle it."

The standard way to handle quotes in strings is to double them.  They 
didn't, so you've got a weird file on your hands.

You can probably fix it by doing a global substitution of all "backslash 
doublequote" pairs with "doublequote doublequote".  Unless they have 
some "backslash backslash doublequote" triples that they want you to 
interpret as "backslash doublequote".  Or some other weirdness.

I'd suggest you try the global subst mentioned above. or ask them for 
data in a reasonably standardized format.

Duncan Murdoch

> best,
> Tim
>>>> David Winsemius <dwinsemius at comcast.net> 1/25/2013 4:16 PM >>>
> On Jan 25, 2013, at 11:35 AM, Tim Howard wrote:
>> Great point, your fix (quote="") works for the example I gave. Unfortunately, these text strings have commas in them as well(!).  Throw a few commas in any of the text strings and it breaks again.  Sorry about not including those in the example.
>> So, I need to incorporate commas *and* quotes with the escape character within a single string.
> Well you need to have _some_ delimiter. At the moment it sounds as though you might end upusing readLines() and strsplit( . , split="\\'\\,\\s\\").
> ______________________________________________
> 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.

More information about the R-help mailing list