Check whether two factors are nested

Share:

Description

This function checks whether two factors are nested, i.e. if each category of the first factor co-occurs with only one category of the other.

Usage

1
is_nested(f1, f2)

Arguments

f1

Numeric vector or factor.

f2

Numeric vector or factor.

Value

Logical, TRUE if factors are nested, FALSE otherwise.

Note

If factors are nested, a message is displayed to tell whether f1 is nested within f2 or vice versa.

References

Grace, K. The Difference Between Crossed and Nested Factors. (web)

See Also

is_crossed

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
# nested factors, each category of
# x appears in one category of y
x <- c(1,2,3,4,5,6,7,8,9)
y <- c(1,1,1,2,2,2,3,3,3)
# show distribution
table(x, y)
# check if nested
is_nested(x, y)
is_nested(y, x)

# not nested factors
x <- c(1,2,3,4,5,6,7,8,9,1,2)
y <- c(1,1,1,2,2,2,3,3,3,2,3)
# show distribution
table(x, y)
# check if nested
is_nested(x, y)
is_nested(y, x)

Want to suggest features or report bugs for rdrr.io? Use the GitHub issue tracker.