recode: manipulate factor levels

Description Usage Arguments Functions See Also Examples

View source: R/recode.R

Description

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

Usage

1
2
3
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

See Also

factor, relevel

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
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/dm documentation built on Jan. 18, 2022, 9:50 a.m.