combine_levels: Combine values

View source: R/utils.R

combine_levelsR Documentation

Combine values

Description

Convenience functions to combine multiple levels of a vector into a new or existing level(s). combine_levels and combine_regex are similar in use, but the latter is more useful for unstructured text which can be grouped by regular expressions.

Usage

combine_levels(
  x,
  levels,
  labels = NULL,
  ordered = is.ordered(x),
  regex = FALSE,
  ...
)

combine_regex(
  x,
  levels,
  labels = NULL,
  ordered = is.ordered(x),
  keep.original = TRUE,
  ...
)

Arguments

x

a vector

levels

for combine_levels, a vector of unique values of x to combine; to combine values into multiple groups, use a list

for combine_regex (or combine_labels(..., regex = TRUE)), a vector of regular expressions; if a list is given, each list element will be collapsed with an "or" statement and treated as single expressions

for values of x which match none of levels and if keep.original = FALSE, a named NULL list element can group these values; otherwise, the smallest unused integer is used; see examples

labels

for combine_levels, a vector of new labels; if levels is a vector, labels should be length 1; if levels is a list, labels (need not be a list but) should have one value for each list element of levels

for combine_regex, if keep.original = FALSE, one additional label should be given for values that do not match any of levels

ordered

logical; if TRUE, returns an ordered factor

regex

logical; if TRUE, levels is assumed to be regular expressions, and inputs are passed to combine_regex

...

additional arguments passed to combine_regex or further to grep

keep.original

deprecated, will be ignored

Value

combine_levels will always return a factor. The levels will be in the order of x plus any new levels (i.e., labels).

combine_regex returns an integer vector if character labels are not given; otherwise, a character vector is returned.

See Also

factor2; recoder

Examples

## combine numeric, character, or factor
x <- rep(1:3, each = 2)
combine_levels(x, 1:2, 1)
combine_levels(x, list(1:2, 3), c('a', 'b'))

## use a named list to get the same as above
combine_levels(x, list(a = 1:2, b = 3))

## use NULL list to combine others
combine_levels(x, list(b = 3, others = NULL))

## levels may be swapped without losing original distinction
combine_levels(x, list('3' = 1:2, '1' = 3))
combine_levels(x, list('1' = 3, '3' = 1:2))


## combine by regular expression
x <- letters[1:5]
combine_regex(x, 'a')
combine_regex(x, c('a', 'b'))
combine_regex(x, c('a', 'b|c|e'))

## character labels return a labeled factor
combine_regex(x, 'a', c('a', 'b'))            ## a -> a; else -> b
combine_regex(x, '[a-c]', c('ABC', 'Others')) ## a,b,c -> ABC; else Others

## levels passed as a list
combine_regex(x, list(ABC = c('a', 'b', 'c')))       ## d,e are unchanged
combine_regex(x, list(ABC = '[a-c]', Others = NULL)) ## d,e are changed

## combine_levels(..., regex = TRUE) returns the same as above
combine_levels(x, '[a-c]', c('ABC', 'Others'), regex = TRUE)


raredd/rawr documentation built on March 4, 2024, 1:36 a.m.