Reputation: 91
I have the following sample data:
library("tidyverse")
raw <- tibble(
emp_name = c("john"),
valid_from = c("2019-01-01"),
valid_to = c("2019-01-03")
)
Below code works when I have only one row in the tibble:
raw <- raw %>%
mutate(periods = paste(c(seq(from = as.Date(valid_from), to = as.Date(valid_to),by = "days")), collapse = ', '))
However, I need to use function which supports more rows.
Upvotes: 1
Views: 76
Reputation: 784
try rowwise()
:
raw %>%
rowwise() %>%
mutate(periods =
paste(c(seq(from = as.Date(valid_from), to = as.Date(valid_to),by = "days")), collapse = ', '))
Source: local data frame [2 x 4]
Groups: <by row>
# A tibble: 2 x 4
emp_name valid_from valid_to periods
<chr> <chr> <chr> <chr>
1 john 2019-01-01 2019-01-03 2019-01-01, 2019-01-02, 2019-01-03
2 max 2019-01-02 2019-01-07 2019-01-02, 2019-01-03, 2019-01-04, 2019-01-05, 2019-01-06,~
Upvotes: 2