I have a dataset in tibble in R like the one below:
# A tibble: 50,045 x 5
ref_key start_date end_date
<chr> <date> <date>
1 123 2010-01-08 2010-01-13
2 123 2010-01-21 2010-01-23
3 123 2010-03-10 2010-04-14
I need to create another tibble that each row only store one date, like the one below:
ref_key date
<chr> <date>
1 123 2010-01-08
2 123 2010-01-09
3 123 2010-01-10
4 123 2010-01-11
5 123 2010-01-12
6 123 2010-01-13
7 123 2010-01-21
8 123 2010-01-22
9 123 2010-01-23
Currently I am writing an explicit loop for that like below:
for (loop in (1:nrow(input.df))) {
if (loop%%100==0) {
print(paste(loop,'/',nrow(input.df)))
}
temp.df.st00 <- input.df[loop,] %>% data.frame
temp.df.st01 <- tibble(ref_key=temp.df.st00[,'ref_key'],
date=seq(temp.df.st00[,'start_date'],
temp.df.st00[,'end_date'],1))
if (loop==1) {
output.df <- temp.df.st01
} else {
output.df <- output.df %>%
bind_rows(temp.df.st01)
}
}
It is working, but in a slow way, given that I have >50k rows to process, it takes a few minutes to finish the loop.
I wonder if this step can be vectorized, is it something related to row_wise
in dplyr
?