Learn R Programming

tidyextreme (version 1.0.0)

calculate_CDD: Calculate consecutive dry days (CDD)

Description

Calculates statistics for dry spells (consecutive days with precipitation < 1 mm), following ETCCDI definition CDD.

Usage

calculate_CDD(
  df,
  frequency = "daily",
  time_col = NULL,
  prcp_col = NULL,
  precip_col = NULL,
  dry_threshold = 1
)

Value

A data.frame with columns: year, CDD_max, CDD_mean, CDD_median, n_dry_spells

Arguments

df

Data frame with precipitation data

frequency

Temporal frequency: "daily" or "hourly" (string)

time_col

Name of the time column (string). Must be in a format recognizable by lubridate (e.g., Date for daily data, POSIXct for hourly data). Recommended formats: - Daily: YYYY-MM-DD (e.g., "2023-01-15") - Hourly: YYYY-MM-DD HH:MM:SS (e.g., "2023-01-15 14:30:00")

prcp_col

Name of precipitation column (daily data) (string)

precip_col

Name of precipitation column (hourly data) (string)

dry_threshold

Threshold for dry day in mm (default: 1)

Examples

Run this code
# Daily precipitation data
daily_prcp <- data.frame(
  date = seq(as.Date("2000-01-01"), as.Date("2002-12-31"), by = "day"),
  rainfall = pmax(0, rgamma(1096, shape = 0.5, scale = 10))
)

# Calculate consecutive dry days statistics
calculate_CDD(
  df = daily_prcp,
  frequency = "daily",
  time_col = "date",
  prcp_col = "rainfall"
)

# With custom dry threshold (0.5mm instead of 1mm)
calculate_CDD(
  df = daily_prcp,
  frequency = "daily",
  time_col = "date",
  prcp_col = "rainfall",
  dry_threshold = 0.5
)

Run the code above in your browser using DataLab