1

Here is a reproducible example of what I want to achieve. Basically I have a datefrom column, dateto column and some value columns and I want to create a time series for each day of the intervals defined with datefrom and dateto. I previously used rowwise, but it is quite slow for larger datasets. Is there a more performant way to do this?

library(tidyverse)
library(lubridate)

# getting some data ready
df <- tibble(datefrom = c("2019-01-01", "2019-01-01"),
       dateto = c("2019-01-02", "2019-01-04"),
       value = 5) %>%
  mutate_if(is.character, ymd)


# preparing the time series
df %>%
  rowwise() %>%
  mutate(Date = list(seq.Date(datefrom, dateto, by = "days")), datefrom = NULL, dateto = NULL) %>%
  unnest()

# how to use pmap
df <- tibble(od = 1:3,
             do = 4:6)

df %>% 
  mutate(seq = pmap(.l = list(od, do), ~ seq(.x, .y)), od = NULL, do = NULL) %>%
  unnest(seq)
Jakub.Novotny
  • 2,912
  • 2
  • 6
  • 21

1 Answers1

1

It may be more efficient to use map2

library(purrr)
library(dplyr)
library(tidyr)
df %>% 
   mutate(Date = map2(datefrom, dateto, ~ seq(.x, .y, by = 'days'))) %>%
   unnest(Date)
akrun
  • 874,273
  • 37
  • 540
  • 662