Skip to contents

Given:

  • a sub-set function f;

  • an object x with its margin m;

  • and a grouping factor grp;

the idx_by() function takes indices per group grp.
The result of idx_by() can be supplied to the indexing arguments (see squarebrackets_indx_args) to perform grouped subset operations.

Usage

idx_by(x, m, f, grp, parallel = FALSE, mc.cores = 1L)

Arguments

x

the object from which to compute the indices.

m

a single non-negative integer giving the margin for which to compute indices.
For flat indices or for non-dimensional objects, use m = 0L.

f

a subset function to be applied per group on indices.
If m == 0L, indices is here defined as setNames(1:length(x), names(x)).
If m > 0L, indices is here defined as setNames(1:dim(x)[m], dimnames(x)[[m]]).
The function must produce a character or integer vector as output.
For example, to subset the last element per group, specify:
f = last

grp

a factor giving the groups.

parallel, mc.cores

see BY.

Value

A vector of indices.

Examples



# vectors ====
(a <- 1:20)
#>  [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20
(grp <- factor(rep(letters[1:5], each = 4)))
#>  [1] a a a a b b b b c c c c d d d d e e e e
#> Levels: a b c d e

# get the last element of `a` for each group in `grp`:
s <- list(idx_by(a, 0L, last, grp))
ss_x(cbind(a, grp), s, 1L)
#>       a grp
#> [1,]  4   1
#> [2,]  8   2
#> [3,] 12   3
#> [4,] 16   4
#> [5,] 20   5


# data.frame ====
x <- data.frame(
  a = sample(1:20),
  b = letters[1:20],
  group = factor(rep(letters[1:5], each = 4))
)
print(x)
#>     a b group
#> 1   4 a     a
#> 2   9 b     a
#> 3   6 c     a
#> 4  13 d     a
#> 5   8 e     b
#> 6  14 f     b
#> 7  11 g     b
#> 8  16 h     b
#> 9   1 i     c
#> 10 20 j     c
#> 11 12 k     c
#> 12 19 l     c
#> 13  3 m     d
#> 14 18 n     d
#> 15 17 o     d
#> 16 15 p     d
#> 17  2 q     e
#> 18  5 r     e
#> 19 10 s     e
#> 20  7 t     e
# get the first row for each group in data.frame `x`:
row <- idx_by(x, 1, first, x$group)
sbt_x(x, row)
#>   a b group
#> 1 4 a     a
#> 2 8 e     b
#> 3 1 i     c
#> 4 3 m     d
#> 5 2 q     e
# get the first row for each group for which a > 10:
x2 <- sbt_x(x, obs = ~ a > 10)
row <- na.omit(idx_by(x2, 1, first, x2$group))
sbt_x(x2, row)
#>    a b group
#> 1 13 d     a
#> 2 14 f     b
#> 3 20 j     c
#> 4 18 n     d