Title: | Count of Sequential Events |
---|---|
Description: | Count the occurrence of sequences of values in a vector that meets certain conditions of length and magnitude. The method is based on the Run Length Encoding algorithm, available with base R, inspired by A. H. Robinson and C. Cherry (1967) <doi:10.1109/PROC.1967.5493>. |
Authors: | Raphael Saldanha [aut, cre] |
Maintainer: | Raphael Saldanha <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.1.1 |
Built: | 2025-01-30 03:52:05 UTC |
Source: | https://github.com/rfsaldanha/nseq |
Shifts vector values to right or left
shift(x, n, invert = FALSE, default = NA)
shift(x, n, invert = FALSE, default = NA)
x |
Vector for which to shift values |
n |
Number of places to be shifted. Positive numbers will shift to the right by default. Negative numbers will shift to the left by default. The direction can be inverted by the invert parameter. |
invert |
Whether or not the default shift directions should be inverted. |
default |
The value that should be inserted by default. |
a vector.
# Lag shift(c(2,3,5,6,7), n = 1, default = 0) # Lead shift(c(2,3,5,6,7), n = -1, default = 0)
# Lag shift(c(2,3,5,6,7), n = 1, default = 0) # Lead shift(c(2,3,5,6,7), n = -1, default = 0)
Given a tibble object and a variable y
, this function will count the number of occurrence of each element in y
in the sequence that they appear, and return this count as a tibble object.
trle(x)
trle(x)
x |
a vector. |
a data.frame
object.
trle(c(8,15,20,0,0,0,0,5,9,12))
trle(c(8,15,20,0,0,0,0,5,9,12))
This function will count the occurrence of sequential events that meets some conditions.
trle_cond(x, a_op = "gte", a, b_op = "gte", b, isolated = FALSE)
trle_cond(x, a_op = "gte", a, b_op = "gte", b, isolated = FALSE)
x |
numeric vector. |
a_op , b_op
|
character. Operator, |
a |
integer. Length of period threshold. |
b |
integer. Value threshold. |
isolated |
logical. Consider only isolated events, i.e. surrounded by zeros. On this case, |
Example: In a vector, how many sequences have at least 3 consecutive observations (a_op = "gte", a = 3
) with values equal or greater than 5 (b_op = "gte", b = 5
)?
a numeric value.
# How many sequences have at least 3 consecutive observations with value equal or greater than 5? trle_cond(x = c(8,15,20,0,0,0,0,5,9,12), a_op = "gte", a = 3, b_op = "gte", b = 5)
# How many sequences have at least 3 consecutive observations with value equal or greater than 5? trle_cond(x = c(8,15,20,0,0,0,0,5,9,12), a_op = "gte", a = 3, b_op = "gte", b = 5)
This function will compute statistics of sequential events that meets some conditions.
trle_cond_stat(x, b, b_op, stat)
trle_cond_stat(x, b, b_op, stat)
x |
numeric vector. |
b |
integer. Value threshold. |
b_op |
character. Operator, |
stat |
character. A statistic to be calculated. One of: max, min, mean, median, sd, var. |
Example: in a vector, what is the maximum size of sequences with values equal or greater than 5?
a numeric value
# What is the maximum size of sequences with values equal or greater than 5? trle_cond_stat(c(4,6,6,4,7,8,9), b = 5, b_op = "gte", stat = "max")
# What is the maximum size of sequences with values equal or greater than 5? trle_cond_stat(c(4,6,6,4,7,8,9), b = 5, b_op = "gte", stat = "max")