Skip to contents

Computes a new factor out of combinations of input factors.


weave_factors(..., drop = TRUE, sep = ".", replaceNA = TRUE)



The vectors


A logical of length 1 which when TRUE will remove combinations of factors not occurring in the input data.


A character of length 1 with a string to delimit the new level labels.


A logical of length 1: replace NA values with empty strings?


A factor representing combinations of input factors.


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.

See also


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]    
#> Levels:
interaction(f1, f2, drop = TRUE, lex.order = FALSE)
#> [1]    
#> Levels:
weave_factors(f1, f2)
#> [1]    
#> Levels:

# The difference is in how characters are interpreted
# The following are equivalent
interaction(f1, f2, drop = TRUE, lex.order = TRUE)
#> [1]    
#> Levels:
weave_factors(as.factor(f1), f2)
#> [1]    
#> Levels: