Skip to contents

Converts sub-daily weather observations to one row per day, optionally within each site or location. The same statistics style used by the window workflow is supported here: use a character vector to apply the same statistics to every variable, or use a named list to choose different statistics for different variables. The special .conditions entry can be used for multivariable summaries within each daily group.

Usage

aggregate_weather_daily(
  weather,
  id_col = NULL,
  time_col = "time",
  date_col = "date",
  weather_cols,
  statistics,
  keep_time = TRUE,
  name_prefix = "daily"
)

Arguments

weather

A weather data frame.

id_col

Optional site, location, or series identifier column. If supplied, aggregation is done separately within each ID.

time_col

Name of the timestamp column. Use NULL when the input data already have a day/date column and no separate timestamp column is needed.

date_col

Name of the day/date column. If this column already exists in weather, it is used for daily grouping. If it does not exist, aggregate_weather_daily() derives it from time_col and creates it in the output.

weather_cols

Required character vector of existing numeric weather columns. Column names must be unique, non-missing, and non-empty. If named, every entry must have a unique, non-missing, non-empty output alias; .conditions is reserved. No columns are inferred. Use character() explicitly for condition-only summaries.

statistics

Required summary specification. Use a non-empty character vector to apply the same statistics to all weather_cols, such as c("mean", "sd", "IQR"). Alternatively, use a named list keyed by every selected column's output alias (or column name when unnamed), with no unknown keys. Each entry must be a non-empty character vector or a nested named list of functions or single function names, for example list(temp = list(avg = "mean", spread = stats::sd)). Statistic names must be unique, non-missing, and non-empty. No summaries are inferred. The optional .conditions entry is a named list of multivariable condition summaries such as count_when(temp >= 18 & rh >= 90).

keep_time

If TRUE, include a daily POSIXct time column at midnight. This requires either time_col or an existing date_col.

name_prefix

Prefix used in aggregated weather columns. The default creates names such as daily_mean_temp and daily_sum_rain.

Value

A daily data frame sorted by ID and date.

Details

The configured ID, time, and date columns cannot be selected in weather_cols. Summary output names must be unique and must not overwrite retained metadata. An input with no rows is rejected.

Examples

hourly <- simulate_weather_series(days = 5, n_series = 2, id_col = "site_id", seed = 1)

aggregate_weather_daily(
  hourly,
  id_col = "site_id",
  weather_cols = c("temp", "rh", "rain"),
  statistics = list(temp = c("mean", "max"), rh = "mean", rain = "sum")
)
#>    site_id       date       time daily_mean_temp daily_max_temp daily_mean_rh
#> 1      S01 2024-01-01 2024-01-01        22.51000          28.83      78.49250
#> 2      S01 2024-01-02 2024-01-02        22.96833          30.32      78.89083
#> 3      S01 2024-01-03 2024-01-03        23.64375          31.27      80.01708
#> 4      S01 2024-01-04 2024-01-04        23.65750          29.58      79.76667
#> 5      S01 2024-01-05 2024-01-05        23.27542          29.56      77.87917
#> 6      S02 2024-01-01 2024-01-01        22.55417          29.42      80.02417
#> 7      S02 2024-01-02 2024-01-02        22.77500          29.74      78.78000
#> 8      S02 2024-01-03 2024-01-03        23.02250          30.12      80.48500
#> 9      S02 2024-01-04 2024-01-04        23.52583          30.77      78.79875
#> 10     S02 2024-01-05 2024-01-05        23.15667          29.59      78.78292
#>    daily_sum_rain
#> 1            3.48
#> 2            1.63
#> 3            3.52
#> 4            1.40
#> 5            3.23
#> 6            3.74
#> 7            1.30
#> 8            4.23
#> 9            8.02
#> 10           3.34