DescTools (version 0.99.15)

Recode: Recode a Factor

Description

Combining or rearranging a factor can be tedious if it has many levels. Recode supports this step by accepting a direct definition of newlevels by oldlevels as argument and adding an "elselevel" option.

Usage

Recode(x, newlevels, elselevel = NA, use.empty = FALSE)

Arguments

x
the factor whose levels are to be altered.
newlevels
a list with the oldlevels combined by c() and named with the name of the new level: list(newlevel_a=c("old_a", "old_b"),newlevel_b=c("old_c", "old_d")). See examples.
elselevel
how should the levels, which are not matched by newlevel's list be named. Set this to NULL, if elselevels should be left unchanged. Defaults to NA.
use.empty
logical. Defines how a new level, which can't be found in x, should be handled. Should it be left in the level's list or be dropped? The default is FALSE, which drops empty levels.

Value

  • the factor having the new levels applied.

See Also

factor, levels There's another possible solution in the package car.

Examples

Run this code
x <- factor(sample(letters, 20))

y <- Recode( x, newlevels=list( 
  "good" = c("a","b","c"),           # the old levels "a","b","c" get the new level "good"
  "bad"  = c("d","e","f"),           # the old levels "d","e","f" get the new level "bad" etc.
  "ugly" = c("g","h","k")), elselevel="other")

data.frame(x, y)



x <- factor(letters[1:6])

z1 <- Recode(x, newlevels=list("AB"=c("a","b"), "CD"=c("c","d")), elselevel="none of these")
z2 <- Recode(x, newlevels=list("AB"=c("a","b"), "CD"=c("c","d")), elselevel=NA)
z3 <- Recode(x, newlevels=list("AB"=c("a","b"), "CD"=c("c","d")), elselevel=NULL)
z4 <- Recode(x, newlevels=list("AB"=c("a","b"), "GH"=c("g","h")), elselevel=NA, use.empty=TRUE)
z5 <- Recode(x, newlevels=list("AB"=c("a","b"), "GH"=c("g","h")), elselevel=NA, use.empty=FALSE)

data.frame(z1, z2, z3, z4, z5)

# empty level GH in z4...
table(z4, useNA="ifany")
# but not in z5
table(z5, useNA="ifany")

Run the code above in your browser using DataCamp Workspace