forcats (version 0.4.0)

fct_cross: Combine levels from two or more factors to create a new factor

Description

Computes a factor whose levels are all the combinations of the levels of the input factors.

Usage

fct_cross(.f, ..., sep = ":", keep_empty = FALSE)

Arguments

.f

A factor or character vector

...

Additional factors or character vectors

sep

A character string to separate the levels

keep_empty

If TRUE, keep combinations with no observations as levels

Value

The new factor

Examples

Run this code
# NOT RUN {
fruit <- factor(c("apple", "kiwi", "apple", "apple"))
colour <- factor(c("green", "green", "red", "green"))
eaten <- c("yes", "no", "yes", "no")
fct_cross(fruit, colour)
fct_cross(fruit, colour, eaten)
fct_cross(fruit, colour, keep_empty = TRUE)
# }

Run the code above in your browser using DataCamp Workspace