I have this dataframe called
mydf
cd
result
mydf<-structure(list(cc = structure(1:3, .Label = c("a", "b", "c"), class = "factor"),
cd = structure(1:3, .Label = c("e,f,g", "f,g,s", "g,h,g"), class = "factor"),
individuals = structure(1:3, .Label = c("apple", "ball",
"cat"), class = "factor")), .Names = c("cc", "cd", "individuals"
), row.names = c(NA, -3L), class = "data.frame")
cc cd individuals
a e apple
a f apple
a g apple
b f ball
b g ball
b s ball
c g cat
c h cat
c g cat
dplyr way
library(stringi)
library(dplyr)
library(tidyr)
mydf %>%
mutate(cd = cd %>% stri_split_fixed(",") ) %>%
unnest(cd)