Learn R Programming

LightLogR (version 0.5.3)

number_states: Number non-consecutive state occurrences

Description

number_states() creates a new column in a dataset that takes a state column and assigns a count value to each state, rising every time a state is replaced by another state. E.g., a column with the states "day" and "night" will produce a column indicating whether this is "day 1", "day 2", and so forth, as will the "night" state with "night 1", "night 2", etc. Grouping within the input dataset is respected, i.e., the count will reset for each group.

Usage

number_states(dataset, state.colname, colname.extension = ".count")

Value

The input dataset with an additional column that counts the occurrences of each state. The new column will of type character

Arguments

dataset

A data.frame with a state column.

state.colname

Column name that contains the state. Expects a symbol. Needs to be part of the dataset. Can be of any type, but character and factor make the most sense.

colname.extension

The extension that is added to the state name to create the new column. Defaults to ".count".

Details

The state column is not limited to two states, but can have as many states as needed. Also, it does not matter in which time frames these states change, so they do not necessarily conform to a 24-hour day. NA values will be treated as their own state.

Gaps in the data can lead to non-sensible outcomes, e.g. if there is no in-between state/observation between a day state at "18:00:00" and a day state at "6:00:00" - this would be counted as day 1 still. In these cases, the gap_handler() function can be useful to a priori add observations.

Examples

Run this code
dataset <- tibble::tibble(
 state =
 c("day", "day", "day", "night", "night", "day", "day", "night",
 "night", "night", "day", "night")
 )
number_states(dataset, state)

#example with photoperiods, calculating the mean values for each day and night
coordinates <- c(48.52, 9.06)
sample.data.environment |> 
  add_photoperiod(coordinates) |> 
  number_states(photoperiod.state) |> 
  dplyr::group_by(photoperiod.state.count, .add = TRUE) |> 
  dplyr::summarize(mean_MEDI = mean(MEDI)) |> 
  tail(13)

Run the code above in your browser using DataLab