-1

I'm reading in a CSV in R using read.csv.

It's a list of countries and their respective identifiers, such as:

Name, ID
Andorra, AD
Russia, RU
"VIRGIN ISLANDS, BRITISH", VG

Reading it in like this will separate on the comma enclosed by the quotation marks, which means that for the 3rd element it will show VIRGIN ISLANDS as the country name, and then BRITISH as the ID.

How do I force it to read in everything encapsulated by the quotation marks as a single element?

Thanks

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
Henry
  • 1,646
  • 12
  • 28
  • Check out this link, could help: http://stackoverflow.com/questions/769621/dealing-with-commas-in-a-csv-file – Atoq Sep 25 '15 at 09:46
  • Thanks - the solution below solves the problem for me for reference – Henry Sep 25 '15 at 09:48

1 Answers1

4

Try

con <- textConnection('Name, ID\nAndorra, AD\nRussia, RU\n"VIRGIN ISLANDS, BRITISH", VG')
csv <- read.csv(con, quote='"')
csv

Actually, this is the default value for the quote, hence I am not sure why you had problem in the first way.

Karsten W.
  • 17,826
  • 11
  • 69
  • 103
  • The answer, as always, is because I'm an idiot. I had quote="", rather than quote='"'. (i.e. two doubles, not single double single). Thanks so much. – Henry Sep 25 '15 at 09:43
  • 1
    Or you can use `"\""` – rbm Sep 25 '15 at 09:51
  • The ultimate solution is to be less of an idiot, really :) quote="" was just a dumb thing to do, had I spent even a minute's brain power I'm sure I would have realised why it was dumb. – Henry Sep 25 '15 at 09:57