I have a dataframe of timeseries data, df1, that I need to extract a number of 'windows' from in R. The start- and end-points for the windows I need are in two columns of a separate dataframe, df2. The values for the start- and end-points correspond to the rownumbers of the windows required.
In the example below I am part of the way to a solution but currently only the first window is extracted. How do I amend this example to extract all four windows? Could this be a case for purrr?
library(tidyverse)
# dataframe of data to subset
df1 <- tibble(my_values = rnorm(100))
# dataframe of windows (i.e. row number IDs) to extract from data
df2 <-tibble::tribble(
~window_start, ~window_end,
3L, 10L,
21L, 25L,
52L, 63L,
78L, 90L
)
# extracted data
df3 <- df1 %>%
slice(df2$window_start : df2$window_end)
(NB. I am aware there is a similar question here - Subset a dataframe using start and stop points from another dataframe? - but my actual data is very large and I am curious whether a non-merge-based solution will be quicker.)