Computes a new factor out of combinations of input factors.
Arguments
- ...
The vectors
- drop
A
logical
of length 1 which whenTRUE
will remove combinations of factors not occurring in the input data.- sep
A
character
of length 1 with a string to delimit the new level labels.- replaceNA
A
logical
of length 1: replaceNA
values with empty strings?
Details
weave_factors()
broadly resembles interaction(..., lex.order = TRUE)
, with a slightly altered approach to non-factor inputs.
In other words, this function orders the new levels such that the levels of
the first input variable in ...
is given priority over the second
input, the second input has priority over the third, etc.
This function treats non-factor inputs as if their levels were
unique(as.character(x))
, wherein x
represents an input.
Examples
f1 <- c("banana", "apple", "apple", "kiwi")
f2 <- factor(c(1, 1:3), labels = c("house", "cat", "dog"))
# Notice the difference in level ordering between the following:
interaction(f1, f2, drop = TRUE, lex.order = TRUE)
#> [1] banana.house apple.house apple.cat kiwi.dog
#> Levels: apple.house apple.cat banana.house kiwi.dog
interaction(f1, f2, drop = TRUE, lex.order = FALSE)
#> [1] banana.house apple.house apple.cat kiwi.dog
#> Levels: apple.house banana.house apple.cat kiwi.dog
weave_factors(f1, f2)
#> [1] banana.house apple.house apple.cat kiwi.dog
#> Levels: banana.house apple.house apple.cat kiwi.dog
# The difference is in how characters are interpreted
# The following are equivalent
interaction(f1, f2, drop = TRUE, lex.order = TRUE)
#> [1] banana.house apple.house apple.cat kiwi.dog
#> Levels: apple.house apple.cat banana.house kiwi.dog
weave_factors(as.factor(f1), f2)
#> [1] banana.house apple.house apple.cat kiwi.dog
#> Levels: apple.house apple.cat banana.house kiwi.dog