Package 'nseq'

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

Help Index


Shifts vector values to right or left

Description

Shifts vector values to right or left

Usage

shift(x, n, invert = FALSE, default = NA)

Arguments

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.

Value

a vector.

Examples

# Lag
shift(c(2,3,5,6,7), n = 1, default = 0)
# Lead
shift(c(2,3,5,6,7), n = -1, default = 0)

Run Length Encoding and return result as a data frame

Description

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.

Usage

trle(x)

Arguments

x

a vector.

Value

a data.frame object.

See Also

rle()

Examples

trle(c(8,15,20,0,0,0,0,5,9,12))

Count the number of events in a sequence

Description

This function will count the occurrence of sequential events that meets some conditions.

Usage

trle_cond(x, a_op = "gte", a, b_op = "gte", b, isolated = FALSE)

Arguments

x

numeric vector.

a_op, b_op

character. Operator, gte = greater than or equal, lte = less than or equal, gt = greater than, lt = less than, e = equal.

a

integer. Length of period threshold.

b

integer. Value threshold.

isolated

logical. Consider only isolated events, i.e. surrounded by zeros. On this case, a and a_op are not considered.

Details

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)?

Value

a numeric value.

Examples

# 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)

Statistics of events in a sequence

Description

This function will compute statistics of sequential events that meets some conditions.

Usage

trle_cond_stat(x, b, b_op, stat)

Arguments

x

numeric vector.

b

integer. Value threshold.

b_op

character. Operator, gte = greater than or equal, lte = less than or equal, gt = greater than, lt = less than, e = equal.

stat

character. A statistic to be calculated. One of: max, min, mean, median, sd, var.

Details

Example: in a vector, what is the maximum size of sequences with values equal or greater than 5?

Value

a numeric value

Examples

# 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")