Here is my data:
structure(list(date = structure(c(19662, 19663, 19664, 19665,
19666), class = "Date"), tmax = c(12, 13, 12, 11, 15)), class = "data.frame", row.names = c(NA,
-5L))
date tmax
2023-11-01 12
2023-11-02 13
2023-11-03 12
2023-11-04 11
2023-11-05 15
What is want to achieve is the following:
date tmax tmax_climate
2023-11-01 12 NA
2023-11-01 12 NA
2023-11-01 12 12
2023-11-01 12 13
2023-11-01 12 12
2023-11-02 13 NA
2023-11-02 13 12
2023-11-02 13 13
2023-11-02 13 12
2023-11-02 13 11
2023-11-03 12 12
2023-11-03 12 13
2023-11-03 12 12
2023-11-03 12 11
2023-11-03 12 15
2023-11-04 11 13
2023-11-04 11 12
2023-11-04 11 11
2023-11-04 11 15
2023-11-04 11 NA
2023-11-05 15 12
2023-11-05 15 11
2023-11-05 15 15
2023-11-05 15 NA
2023-11-05 15 NA
Basically what I want is to get tmax
from two previous days and tmax
for two next days. I've tried rollapply()
, lead()
and lag()
but with no luck so far. Preferably I would like to stick to dplyr
. Order doesn't matter in tmax_climate
(no need to be tmax
-1, tmax
, tmax
+1 as in desired output)
Result