Одним из способов использования tidyverse
может быть создание последовательности между date_start
и date_end
, объединение с dat4
с использованием "StationCode"
, filter
строк, находящихся в диапазоне (то есть между началом и концом дата), group_by
event
, date_start
, date_end
и StationCode
для расчета mean
.
library(tidyverse)
events %>%
mutate(date = map2(date_start, date_end, seq, by = "day")) %>%
unnest(date) %>%
left_join(dat4, by = 'StationCode') %>%
filter(DateFormatted >= date_start & DateFormatted <= date_end) %>%
group_by(event_no, date_start, date_end, StationCode) %>%
summarise(Sal = mean(Sal))
# event_no date_start date_end StationCode Sal
# <int> <date> <date> <fct> <dbl>
#1 1 2003-01-01 2003-01-04 niwtawq 1.41
#2 2 2003-01-06 2003-01-10 niwtawq 1.63
данные
events <- structure(list(event_no = 1:6, duration = c(4L, 5L, 7L, 6L, 7L,
5L), date_start = structure(c(12053, 12058, 12563, 12722, 13362,
13732), class = "Date"), date_end = structure(c(12056, 12062,
12569, 12727, 13368, 13736), class = "Date"), StationCode =
structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = "niwtawq", class = "factor")),
row.names = c("1", "2", "3", "4", "5", "6"), class = "data.frame")
dat4 <- structure(list(StationCode = structure(c(1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L), .Label = "niwtawq", class = "factor"),
DateFormatted = structure(c(12053, 12054, 12055, 12056, 12057, 12058, 12059, 12060,
12061, 12062), class = "Date"), Sal = c(1.58, 1.19, 1.31, 1.56, 2.1, 1.33,
1.68, 1.83, 1.77, 1.56)), row.names = c("1:", "2:", "3:", "4:",
"5:", "6:", "7:", "8:", "9:", "10:"), class = "data.frame")