Learn R Programming

tidyextreme (version 1.0.0)

calculate_TR20: Calculate number of tropical nights (TN > 20\(^\circ\)C)

Description

Counts the number of days per year when daily minimum temperature exceeds 20\(^\circ\)C, following ETCCDI definition TR20.

Usage

calculate_TR20(
  df,
  frequency = "daily",
  time_col = NULL,
  tmin_col = NULL,
  temp_col = NULL,
  threshold = 20
)

Value

A tibble with columns: year, TR20

Arguments

df

Data frame with climate data

frequency

Temporal frequency: "daily" or "hourly"

time_col

Name of the time column. For daily frequency, the column should be of class Date or a string in the format YYYY-MM-DD. For hourly frequency, the column should be of class POSIXct or a string in the format YYYY-MM-DD HH:MM:SS.

tmin_col

Name of minimum temperature column (daily data)

temp_col

Name of temperature column (for single temp or hourly)

threshold

Temperature threshold in \(^\circ\)C (default: 20)

Examples

Run this code
# Daily data with separate min temperature
daily_data <- data.frame(
  date = seq(as.Date("2000-01-01"), as.Date("2002-12-31"), by = "day"),
  tmin = rnorm(1096, mean = 18, sd = 5)
)

calculate_TR20(
  df = daily_data,
  frequency = "daily",
  time_col = "date",
  tmin_col = "tmin"
)

# Hourly data (will be aggregated to daily min temperature)
hourly_data <- data.frame(
  datetime = seq(
    as.POSIXct("2000-01-01 00:00", tz = "UTC"),
    as.POSIXct("2000-01-31 23:00", tz = "UTC"),
    by = "hour"
  ),
  temperature = rnorm(31*24, mean = 16, sd = 3)
)

calculate_TR20(
  df = hourly_data,
  frequency = "hourly",
  time_col = "datetime",
  temp_col = "temperature",
  threshold = 20
)

Run the code above in your browser using DataLab