0

I am trying to import a CSV file with R, and am having trouble.

The CSV entries look like

"Name 1" , "Name 2, Name 3, Name 4"

If I import straight to R, the data is read in like

Name 1     Name 2,Name 3,Name 4

but I would like it to look like

Name 1     Name 2
Name 1     Name 3
Name 1     Name 4

Is there a way to break up the second column during import so I can have two columns with only one name in each?

Thanks

1 Answers1

0

Use strsplit to break the column up into an actual list class, then use tidyr::unnest to get your result:

d = data.frame(a = "Name 1" , b = "Name 2, Name 3, Name 4")
d$b = strsplit(d$b, ",")
library(tidyr)
unnest(d)
#        a       b
# 1 Name 1  Name 2
# 2 Name 1  Name 3
# 3 Name 1  Name 4

Depending on how clean things are, you might need to trim whitespace of the new column, see ?trimws for help there.

Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294