Search code examples
rdata-cleaning

How to spread a tibble conbined with strsplit?


I need to spread a tibble where one column has many elements to split, and they're not the same length. I put the orginal sample and predicted outcome below. Thanks.

a;b;c | 2016
d;e  | 2019

converts it into:

a | 2016
b | 2016
c | 2016
d | 2019
e | 2019 

Solution

  • Maybe using strsplit and use Map with data.frame and rbind the result:

    do.call(rbind, Map(data.frame, a=strsplit(x$a, ";", TRUE), b=x$b))
    #  a    b
    #1 a 2016
    #2 b 2016
    #3 c 2016
    #4 d 2019
    #5 e 2019
    

    or

    y <- strsplit(x$a, ";", TRUE)
    data.frame(a = unlist(y), b = rep(x$b, lengths(y)))
    #  a    b
    #1 a 2016
    #2 b 2016
    #3 c 2016
    #4 d 2019
    #5 e 2019
    

    Data:

    x <- data.frame(a = c("a;b;c", "d;e"), b = c(2016, 2019))