0

I have a multiple countries in a cell. I need duplicate rows for each country. Would prefer dplyr/tidyr if possible.

Original data:

df_in <- data.frame(Invoice = c("12345","54321"),
                    Dollar = c(1000,2000),
                     Country = c("Argentina, Chile, Colombia, Mexico, Venezuela","Japan, China, Korea"))
df_in
#  Invoice Dollar                                       Country
#1   12345   1000 Argentina, Chile, Colombia, Mexico, Venezuela
#2   54321   2000                           Japan, China, Korea

Output needed:

df_out <- data.frame(Invoice = c("12345","12345","12345","12345","12345","54321","54321","54321"),
                 Dollar = c(1000,1000,1000,1000,1000,2000,2000,2000),
                 Country = c("Argentina","Chile","Colombia","Mexico","Venezuela","Japan","China","Korea"))
df_out

#  Invoice Dollar   Country
#1   12345   1000 Argentina
#2   12345   1000     Chile
#3   12345   1000  Colombia
#4   12345   1000    Mexico
#5   12345   1000 Venezuela
#6   54321   2000     Japan
#7   54321   2000     China
#8   54321   2000     Korea 
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
jayfrost
  • 1
  • 1

0 Answers0