recode: manipulate factor levels

View source: R/recode.R

recodeR Documentation

manipulate factor levels

Description

This functions enables relabeling, reordering and merging of factor levels.

Usage

recode(x, L, asFactor = TRUE, newFirst = TRUE)

dcode(...)

Arguments

x

factor or character vector to be relabelled or reordered

L

a list specifying the relabeling/reordering, list names specify a new (or old) name and the list elements are vectors of (old) values that are to be assigned that name. The order of L is the order of the levels (with old values 'outside' the list either after of before, depending on the newFirst value). List elements =NULL just means that this name is to be unchanged but be placed in that place (order-wise).

asFactor

if TRUE then a factor is returned (default TRUE)

newFirst

if TRUE then added levels will be placed first in the list of levels (default TRUE)

...

dcode arguments passed to recode

Functions

  • dcode(): 'dcode' is an alias for 'recode' since the latter is in use in some proper R package

See Also

factor, relevel

Examples

x <- LETTERS[2:1]
factor(x) ## levels in alphabetical order
recode(x, L = list('B' = NULL)) ## makes 'B' be the first level
x <- c('', LETTERS[1:5], NA)
factor(x)
L <- list('new_BC_or_NA' = c('B', 'C', NA),
          'E' = NULL,
          'new_D_(level_after_E)' = 'D',
          'blank' = '')
y <- recode(x, L, newFirst = FALSE) ## all unspecified values comes first
levels(y)
table(y, x, useNA = 'ifany')

renlund/datma documentation built on June 2, 2025, 5:12 a.m.