set.seed(123)
dat <- data.frame(day = 1:365, rain = runif(min = 0, max = 5,365),tmean = runif(min = 15, max = 33, 365) )
dat <- dat %>% mutate(mean.daily.rain = mean(rain),mean.daily.tmean = mean(tmean)) %>%
mutate(rain.acc = rain - mean.daily.rain,tmean.acc = tmean - mean.daily.tmean)
If I want to find which day of the year the cumsum value of rain.acc
or tmean.acc
was the minimum I can do this:
dat %>% summarise(which.min(cumsum(rain.acc)))
329
dat %>% summarise(which.min(cumsum(tmean.acc)))
159
However, I want to impose a condition that I only want to look at the doy >= 213 and <= 365 i.e. how do I extract the day of year between 213 and 365 with the lowest value of cumsum(rain.acc)
and cumsum(tmean.acc)
. Note that cumsum
has to be calculated over the entire year.