smashlevels: Collapse levels of a factor

Description Usage Arguments Examples

View source: R/utils.R

Description

Combines levels of a factor into new levels

Usage

1
smashlevels(f, newlevels)

Arguments

f

factor.

newlevels

list. The names of newlevels are the new levels. Each list element is a list of old levels in the factor f which should be combined into the new level

Examples

1
2
3
4
5
6
7
8
# create a factor with levels 30:60
age <- factor(sample(30:60, 200, replace=TRUE))
# combine 35-40 into a single level, 41-50 into a single level, and 51-60 into a single level
g <- smashlevels(age, list(`35-40` = 35:40, `41-50` = 41:50, `51-60` = 51:60))
table(g)
# If the syntax permits, the backticks can be avoided.
h <- smashlevels(age, list(young=30:34, pushing40 = 35:40, pushing50 = 41:50, fossilized = 51:120))
table(h)

durmod documentation built on March 31, 2020, 5:23 p.m.