weave_factors: Bind together factors

View source: R/conveniences.R

weave_factorsR Documentation

Bind together factors

Description

Computes a new factor out of combinations of input factors.

Usage

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

Arguments

...

The vectors

drop

A logical of length 1 which when TRUE 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: replace NA 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.

Value

A factor representing combinations of input factors.

See Also

interaction()

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)
interaction(f1, f2, drop = TRUE, lex.order = FALSE)
weave_factors(f1, f2)

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

ggh4x documentation built on May 29, 2024, 6:47 a.m.