Learn R Programming

tidyextreme (version 1.0.0)

calculate_PRCPstats: Calculate annual precipitation totals and statistics

Description

Calculates comprehensive annual precipitation statistics including total precipitation, number of wet days, mean daily precipitation, and maximum daily precipitation.

Usage

calculate_PRCPstats(
  df,
  frequency = "daily",
  time_col = NULL,
  prcp_col = NULL,
  precip_col = NULL,
  wet_threshold = 1
)

Value

A data.frame with columns: year, PRCP_total, PRCP_days, PRCP_mean, PRCP_max

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)

wet_threshold

Threshold for wet 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 comprehensive precipitation statistics
calculate_PRCPstats(
  df = daily_prcp,
  frequency = "daily",
  time_col = "date",
  prcp_col = "rainfall"
)

# With custom wet threshold (2mm instead of 1mm)
calculate_PRCPstats(
  df = daily_prcp,
  frequency = "daily",
  time_col = "date",
  prcp_col = "rainfall",
  wet_threshold = 2
)

# Hourly precipitation data (converted to daily)
hourly_prcp <- data.frame(
  datetime = seq(
    as.POSIXct("2000-01-01 00:00", tz = "UTC"),
    as.POSIXct("2000-01-31 23:00", tz = "UTC"),
    by = "hour"
  ),
  precip = pmax(0, rgamma(31*24, shape = 0.3, scale = 2))
)

calculate_PRCPstats(
  df = hourly_prcp,
  frequency = "hourly",
  time_col = "datetime",
  precip_col = "precip"
)

Run the code above in your browser using DataLab