[R] read.csv quotes within fields
tghoward at gw.dec.state.ny.us
Mon Jan 28 13:39:33 CET 2013
Yes! This does this trick. Thank You!
>>> peter dalgaard <pdalgd at gmail.com> 1/26/2013 11:49 AM >>>
On Jan 26, 2013, at 16:32 , Tim Howard wrote:
> Good point - I guess I am expecting too much. I'll work on a global replace before import or chopping with strsplit while importing.
> FYI everyone - these folks with the non-standard csv are the US Feds:
> (see off-airport AEA 2005 for a csv with issues.)
Does this do the trick?
dd <- read.csv(text=gsub("\\\"", "\"\"", fixed=TRUE,
> Thank you for the help. I really do appreciate the suggested solutions.
> Also, thanks to John Kane for the link to csvEdit.
>>>> Duncan Murdoch <murdoch.duncan at gmail.com> 01/25/13 6:37 PM >>>
> On 13-01-25 4:37 PM, Tim Howard wrote:
>> 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
>>>>> 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
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
> R-help at r-project.org mailing list
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com
More information about the R-help