Skip to contents

Use define_windows() when you already know which periods you want to summarise. You supply one pair of offsets per period. The same offsets are applied to each assessment date, and periods can share days.

Start with weather and assessment tables

Here we have daily rain at two sites and an assessment on January 20 at each site. We set one rain reading at site A to NA to show how the coverage table reports it.

library(windcut)

dates <- seq(as.Date("2024-01-05"), as.Date("2024-01-17"), by = "day")
weather <- rbind(
  data.frame(site_id = "A", time = dates, rain = c(1:5, NA, 7:13)),
  data.frame(site_id = "B", time = dates, rain = rep(1, length(dates)))
)
assessments <- data.frame(
  site_id = c("A", "B"),
  assessment_time = as.Date("2024-01-20")
)

knitr::kable(weather[weather$site_id == "A", ], caption = "Rain readings at site A")
Rain readings at site A
site_id time rain
A 2024-01-05 1
A 2024-01-06 2
A 2024-01-07 3
A 2024-01-08 4
A 2024-01-09 5
A 2024-01-10 NA
A 2024-01-11 7
A 2024-01-12 8
A 2024-01-13 9
A 2024-01-14 10
A 2024-01-15 11
A 2024-01-16 12
A 2024-01-17 13
knitr::kable(assessments, caption = "Assessment dates")
Assessment dates
site_id assessment_time
A 2024-01-20
B 2024-01-20

Enter the two periods

Negative offsets are days before the assessment when you use unit = "days". The start is included and the end is excluded. The first window uses January 5 through 9. The second uses January 9 through 16. Both include January 9, and its rain reading contributes to both totals.

windows <- define_windows(list(
  c(-15, -10),
  c(-11,  -3)
))

knitr::kable(windows, caption = "The two windows you selected")
The two windows you selected
relative_start relative_end width label
-15 -10 5 window_m15_m10
-11 -3 8 window_m11_m03
plot_window_pane(windows, title = "Two periods before assessment",
                 xlab = "Days relative to assessment")

Two overlapping periods before assessment, from day minus 15 to minus 10 and from day minus 11 to minus 3.

Summarise the weather and check coverage

Pass the windows to window_pane() and specify that you expect one reading per day. The two tables have the same assessments in the same order.

result <- window_pane(
  weather = weather,
  assessments = assessments,
  windows = windows,
  id_col = "site_id",
  weather_cols = "rain",
  statistics = "sum",
  unit = "days",
  sampling_interval = 1
)
## 1 weather predictor(s) have missing data. Set `max_allowed_missing_pct` to drop predictors above a limit.
knitr::kable(result$features, caption = "Rain totals by site and window")
Rain totals by site and window
site_id assessment_time rain_sum_window_m15_m10 rain_sum_window_m11_m03
A 2024-01-20 15 62
B 2024-01-20 5 8
knitr::kable(result$coverage, caption = "Observation counts and missing percentages")
Observation counts and missing percentages
site_id assessment_time n_expected_window_m15_m10 n_obs_window_m15_m10 missing_pct_rain_sum_window_m15_m10 n_expected_window_m11_m03 n_obs_window_m11_m03 missing_pct_rain_sum_window_m11_m03
A 2024-01-20 5 5 0 8 8 12.5
B 2024-01-20 5 5 0 8 8 0.0

At site A, the first total is 15 mm. The second is 62 mm from seven of the eight expected readings, so its missing percentage is 12.5%. No predictor is dropped by default. If you rerun the same call with max_allowed_missing_pct = 10, the second rain-total column is dropped for both sites. The coverage table still shows why.

To use names of your own, name the list entries. The names become the window labels and are used in the feature column names.

knitr::kable(define_windows(list(early = c(-15, -10), late = c(-11, -3))))
relative_start relative_end width label
-15 -10 5 early
-11 -3 8 late