spore234
spore234

Reputation: 3640

Read csv but skip escaped commas in strings

I have a csv file like this:

id,name,value
 1,peter,5
 2,peter\,paul,3

How can I read this file and tell R that "\," does not indicate a new column, only ",".

I have to add that file has 400mb.

Thanks

Upvotes: 3

Views: 1271

Answers (1)

Thomas
Thomas

Reputation: 44525

You can use readLines() to read the file into memory and then pre-process it. If you're willing to convert the non-separate commas into something else, you can do something like:

> read.csv(text = gsub("\\\\,", "-", readLines("dat.csv")))
  id       name value
1  1      peter     5
2  2 peter-paul     3

Another option is to utilize the fact that the fread function from data.table can perform system commands as its first argument. Then you can do something like a sed operation on the file before reading it in (which may or may not be faster):

> data.table::fread("sed -e 's/\\\\\\,/-/g' dat.csv")
   id       name value
1:  1      peter     5
2:  2 peter-paul     3

You can always then use gsub() to convert the temporary - separator back into a comma.

Upvotes: 4

Related Questions