I have a data frame containing \"name\"
of U.S. Presidents, the years when they start and end in office, (\"from\"
and \"to\"
columns
Another solution using dplyr
and tidyr
:
library(magrittr) # for pipes
df <- data.frame(tata = c('toto1', 'toto2'), from = c(2000, 2004), to = c(2001, 2009))
# tata from to
# 1 toto1 2000 2001
# 2 toto2 2004 2009
df %>%
dplyr::as.tbl() %>%
dplyr::rowwise() %>%
dplyr::mutate(combined = list(seq(from, to))) %>%
dplyr::select(-from, -to) %>%
tidyr::unnest(combined)
# tata combined
#
# 1 toto1 2000
# 2 toto1 2001
# 3 toto2 2004
# 4 toto2 2005
# 5 toto2 2006
# 6 toto2 2007
# 7 toto2 2008
# 8 toto2 2009