Uses a vectory specifying whether data falls into an event to reshape data, aligning by the onset of the event

align_events(
  df,
  df_isevent,
  dovars,
  leng_threshold,
  before,
  after,
  do_norm = FALSE,
  nbins = 6,
  normbin = 2
)

Arguments

df

A data frame containing all data continuously along time, required columns: "site", "date".

df_isevent

A data frame nrow(df_isevent)==nrow(df) specifying whether respective dates (matching dates in df), satisfy a condition that is used to define events. Events are then derived by this function as consecutive dates where this condition is satisfied (the minimum length for defining an event is given by argument leng_threshold). Required columns in both df and df_isevent: "site", "date"

dovars

A vector of character strings specifying which columns (by column name) of df to re-arrange.

leng_threshold

An integer specifying the minimum number of consecutive dates required to define an event. All events of length lower than leng_threshold are dropped.

before

An integer specifying the number of days before the event onset to be retained in re-arranged data

after

An integer specifying the number of days after the event onset to be retained in re-arranged data

do_norm

A logical specifying whether re-arranged data is to be normalised by the median value of the bin (number of bins given by argument nbins) before the event onset, given by argument normbin. Defaults to FALSE.

nbins

An integer, specifying the number of bins used to determine median values before event onset. Only used when codedo_norm=TRUE. Defaults to 6.

normbin

An integer, specifying the bin number just before the event onset, used for normalisation. Only used when codedo_norm=TRUE. Defaults to 2.

Value

A named list of data frames (list( "df_dday", "df_dday_aggbydday")) containing data from all events and before + after dates (relative to event onset) with additional columns named "inst", defining the event number (instance), and "dday", defining the date relative to the respective event onset. The data frame "df_dday" contains rearranged, but otherwise unchanged data (unless do_norm=TRUE). The data frame "df_dday_aggbydday" containes data aggregated across events with the mean and quantiles given for each "dday".

Examples

df_alg <- align_events( df, truefalse, before=30, after=300 )
#> Error in df %>% left_join(df_isevent, by = c("site", "date")) %>% mutate(idx_df = 1:n()): could not find function "%>%"