Computes a factor whose levels are all the combinations of the levels of the input factors.
Arguments
- ...
<
dynamic-dots
> Additional factors or character vectors.- sep
A character string to separate the levels
- keep_empty
If TRUE, keep combinations with no observations as levels
Examples
fruit <- factor(c("apple", "kiwi", "apple", "apple"))
colour <- factor(c("green", "green", "red", "green"))
eaten <- c("yes", "no", "yes", "no")
fct_cross(fruit, colour)
#> [1] apple:green kiwi:green apple:red apple:green
#> Levels: apple:green kiwi:green apple:red
fct_cross(fruit, colour, eaten)
#> [1] apple:green:yes kiwi:green:no apple:red:yes apple:green:no
#> Levels: apple:green:no kiwi:green:no apple:green:yes apple:red:yes
fct_cross(fruit, colour, keep_empty = TRUE)
#> [1] apple:green kiwi:green apple:red apple:green
#> Levels: apple:green kiwi:green apple:red kiwi:red