| vec_group {vctrs} | R Documentation |
Identify groups
Description
-
vec_group_id()returns an identifier for the group that each element ofxfalls in, constructed in the order that they appear. The number of groups is also returned as an attribute,n. -
vec_group_loc()returns a data frame containing akeycolumn with the unique groups, and aloccolumn with the locations of each group inx. -
vec_group_rle()locates groups inxand returns them run length encoded in the order that they appear. The return value is a rcrd object with fields for thegroupidentifiers and the runlengthof the corresponding group. The number of groups is also returned as an attribute,n.
Usage
vec_group_id(x)
vec_group_loc(x)
vec_group_rle(x)
Arguments
x |
A vector |
Value
-
vec_group_id(): An integer vector with the same size asx. -
vec_group_loc(): A two column data frame with size equal tovec_size(vec_unique(x)).A
keycolumn of typevec_ptype(x)A
loccolumn of type list, with elements of type integer.
-
vec_group_rle(): Avctrs_group_rlercrd object with two integer vector fields:groupandlength.
Note that when using vec_group_loc() for complex types, the default
data.frame print method will be suboptimal, and you will want to coerce
into a tibble to better understand the output.
Dependencies
Examples
purrr <- c("p", "u", "r", "r", "r")
vec_group_id(purrr)
vec_group_rle(purrr)
groups <- mtcars[c("vs", "am")]
vec_group_id(groups)
group_rle <- vec_group_rle(groups)
group_rle
# Access fields with `field()`
field(group_rle, "group")
field(group_rle, "length")
# `vec_group_id()` is equivalent to
vec_match(groups, vec_unique(groups))
vec_group_loc(mtcars$vs)
vec_group_loc(mtcars[c("vs", "am")])
if (require("tibble")) {
as_tibble(vec_group_loc(mtcars[c("vs", "am")]))
}