How to use tidyr::separate when the number of needed variables is unknown [duplicate]

This is a good question – my usual repsonse is to use strsplit, then unnest and spread, which is also not super efficient:

library(dplyr)
library(tidyr)

dat %>% mutate(to = strsplit(to, ",")) %>%
        unnest(to) %>%
        group_by(from) %>%
        mutate(row = row_number()) %>%
        spread(row, to)

Source: local data frame [4 x 5]

                 date                from                   1                   2                 3
               (time)               (chr)               (chr)               (chr)             (chr)
1 2015-10-22 15:03:17   [email protected]   [email protected] [email protected]                NA
2 2015-10-22 15:03:17   [email protected] [email protected]                  NA                NA
3 2015-10-22 15:03:17 [email protected]     [email protected]   [email protected] [email protected]
4 2015-10-22 15:03:17     [email protected]   [email protected]                  NA                NA

Leave a Comment