1

I have a dataframe as such:

Col1  Col2 
a     2
b     1, 3   
c     4

I want to duplicate the second row so that in Col2, there is only one of the elements. For example,

Col1  Col2 
a     2
b     1
b     3  
c     4

I think I would have to use strsplit() but I'm not entirely sure how I'd implement this.

1 Answers1

1

If it is a string column, we can make use separate_rows

library(tidyverse)
df1 %>%
   separate_rows(Col2, convert = TRUE)
#   Col1 Col2
#1    a    2
#2    b    1
#3    b    3
#4    c    4

data

df1 <- structure(list(Col1 = c("a", "b", "c"), Col2 = c("2", "1, 3", 
"4")), class = "data.frame", row.names = c(NA, -3L))
akrun
  • 874,273
  • 37
  • 540
  • 662