summarise_along_walk: Summarise every n numbers in a vector

View source: R/sims.R

summarise_along_walkR Documentation

Summarise every n numbers in a vector

Description

This function summarises every n numbers in a vector.

Usage

summarise_along_walk(vec, every, summarise = sum, na.rm = FALSE, ...)

Arguments

vec

A numeric vector.

every

An integer that defines the step length of the walk over the vector.

summarise

A function that summarises the numbers in each step.

na.rm

A logical value that defines whether or not to remove NAs.

...

Additional arguments passed to summarise.

Value

The function returns a numeric vector.

Source

This function is a slight modification of the code provided here: https://stackoverflow.com/questions/43635846/calculating-mean-for-every-n-values-from-a-vector.

Examples

## Not run: 
x <- c(rep(1, 10), rep(2, 10))
summarise_along_walk(x, every = 10)
x <- c(mean(10, 5), mean(100, 5))
summarise_along_walk(x, every = 10, summarise = mean)
x <- c(x, NA)
summarise_along_walk(x, every = 10, summarise = mean, na.rm = TRUE)

## End(Not run)


edwardlavender/flapper documentation built on Jan. 22, 2025, 2:44 p.m.