Assign Values to Groups of Values
Arguments
- x
vector of values
- assignments
list of assignments of the form \<key\> = \<values\> with \<values\> being a vector of elements to be looked up in
x
and to be replaced with \<key\> in the output vector- ignore.case
if
TRUE
the case is ignored when comparing values- to.factor
if
TRUE
the new values are converted tofactor
. The default isFALSE
.- to.numeric
if
TRUE
(the default!) and independent ofto.factor
(!) the returned values are converted to numeric values if all assigned (even though string) values "look like" numeric values, such as "1", "2", "3.4", "5.67".
Value
vector with as many elements as there are elements in x
. The
vector contains \<key\> at positions where the elements in x
appeared
in the vector \<values\> of a \<key\> = \<values\> assignment of
assignments
Examples
regroup(c("A", "B", "C", "D"), assignments = list(
"AB" = c("A", "B"),
"CD" = c("C", "D")
))
#> [1] "AB" "AB" "CD" "CD"
x <- c("A", "B", "C", "D", "E", "A")
assignments <- list(
"1" = c("A", "B"),
"2" = c("C", "D")
)
regroup(x, assignments)
#> [1] 1 1 2 2 NA 1
# to.factor is ignored...
regroup(x, assignments, to.factor = TRUE)
#> [1] 1 1 2 2 NA 1
# ... unless to.numeric is FALSE!
regroup(x, assignments, to.factor = TRUE, to.numeric = FALSE)
#> [1] 1 1 2 2 <NA> 1
#> Levels: 1 2