Create a numeric index over the unique levels of a variable or a set of variables.

group_index(..., levels = NULL)

Arguments

...

Vectors of equal length.

levels

A list with the order of the unique levels for each input vector (indices assigned from first to last level).

Value

An integer vector from 1 to the number of unique levels.

Examples

# Convert to numeric index
group_index( rep( LETTERS[3:1], each = 3 ) )
#> [1] 3 3 3 2 2 2 1 1 1

# Can control assignment of indices
group_index(
  rep( LETTERS[3:1], each = 3 ), levels = list( c( 'C', 'B', 'A' ) )
)
#> [1] 1 1 1 2 2 2 3 3 3

# Can create single index over all
# unique combinations of multiple variables
group_index( rep( LETTERS[1:3], each = 3 ), rep( 1:3, 3 ) )
#> [1] 1 2 3 4 5 6 7 8 9