group_if: Group vector values

Description Usage Arguments Details Value Examples

View source: R/group_if.R

Description

Split a vector or a list into groups, given a predicate function.

Usage

1
2
3
group_if(x, predicate, na.rm = FALSE)

group_eq(x, na.rm = FALSE)

Arguments

x

a vector or a list to split into groups.

predicate

a binary function returning a boolean value.

na.rm

if x is atomic, delete missing values before grouping.

Details

predicate will be applied to 2 adjacent elements. If it evaluates to TRUE, those elements belong to the same group, otherwise they belong to different groups.

Grouping on equality is the most natural approach, therefore group_eq is a convenient shortcut defined as

group_if (resp. group_eq) is inspired by groupBy (resp. group) in Haskell. Note that group_if behaves a little differently : while in Haskell, the comparison is made with the first element in the group, in this R-version the comparison is made with the adjacent element.

The operator %on% may be helpful to create a predicate with readable syntax.

Value

A list where each element is a group (flattening this list should give back the same values in the same order). Element names are kept.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
x1 <- c(3, 4, 2, 2, 1, 1, 1, 3)
group_eq(x1)
group_if(x1, `<=`)
group_if(x1, function(x, y) abs(x - y) > 1)

x2 <- c(3, 4, 2, -2, -1, 1, 1, 3)
group_if(x2, `==` %on% abs)

x3 <- list(1:3, 1:3, 3:5, 1, 2)
group_if(x3, `==` %on% length)

funprog documentation built on Jan. 13, 2021, 11:52 a.m.