split - In R: get multiple rows by splitting a column using tidyr and reshape2 -
this question has answer here:
what simpel way using tidyr
or reshape2
turn data:
data <- data.frame( a=c(1,2,3), b=c("b,g","g","b,g,q"))
into (e.g. make row each comma separated value in variable b
):
b 1 1 b 2 1 g 3 2 g 4 3 b 5 3 g 6 3 q
try
library(splitstackshape) csplit(data, 'b', ',', 'long')
or using base r
lst <- setnames(strsplit(as.character(data$b), ','), data$a) stack(lst)
or
library(tidyr) unnest(lst,a)
Comments
Post a Comment