Harmonize a factor
Arguments
- .data
(
character
or coercible tocharacter
) A vector to harmonize to the specified factor.- ...
These dots are for future extensions and must be empty.
- .spec
(
hrmn_spec_fct
) A harmonization specification fromspecify_fct()
.- .lookup
(named
character
) A vector of replacement values. The names are the values in.data
and the values are the target values.
Value
A harmonized factor()
.
See also
Other harmonization functions:
harmonize_df()
Examples
# Without a spec, harmonize_fct() acts like [base::factor()].
harmonize_fct(c("a", "b", "c"))
#> [1] a b c
#> Levels: a b c
# Basic harmonization, dropping levels not in the spec
spec <- specify_fct(levels = c("a", "b"))
harmonize_fct(c("a", "b", "c"), .spec = spec)
#> [1] a b <NA>
#> Levels: a b
# Using a lookup table to recode values
spec2 <- specify_fct(levels = c("fruit", "citrus"))
lookup <- c(apple = "fruit", banana = "fruit", orange = "citrus")
harmonize_fct(
c("apple", "banana", "orange"),
.spec = spec2,
.lookup = lookup
)
#> [1] fruit fruit citrus
#> Levels: fruit citrus