Skip to contents

Identifies and extracts events in a time series based on threshold criteria, with options for event selection and characterization.

Usage

apply_threshold(
  X,
  lim,
  where = "<=",
  what = "X",
  select = "all",
  Date = NULL,
  period = NULL
)

Arguments

X

Numeric vector of values to analyze (typically discharge or other hydro-meteorological variable)

lim

Numeric threshold value or vector of same length as X

where

String specifying threshold condition:

  • "<", "<=", "==", ">=", ">" for standard comparisons

what

String specifying return type:

  • "X": Values meeting condition

  • "length": Duration of events (time steps)

  • "first"/"last": First/last index of events

  • Any function name (e.g., "which.max"): Applied to event values

select

String or numeric specifying event selection:

  • "all": All events (default)

  • "longest"/"shortest": Longest/shortest event

  • Numeric value: Event containing this value

Date

Optional date vector for period filtering

period

Optional vector of 2 dates (start/end) for period filtering

Value

Depends on what parameter:

  • Numeric vector of values ("X")

  • Integer duration ("length")

  • Single index ("first"/"last")

  • Function application result

  • NA if no events found

Details

Handles multiple threshold scenarios:

  • For vector lim, uses most frequent value via rle()

  • For period filtering, subsets data before analysis

  • For event selection, identifies contiguous periods

Examples

set.seed(1)
Q <- rnorm(100, 50, 10) # Simulated discharge

# Get longest drought period (Q <= 45)
drought_days <- apply_threshold(Q, 45, where="<=", what="length", select="longest")

# Get first index of all flood events (Q >= 60)
flood_starts <- apply_threshold(Q, 60, where=">=", what="first")