These functions checks whether two factors are (fully) crossed or nested, i.e. if each level of one factor occurs in combination with each level of the other factor (is_crossed()) resp. if each category of the first factor co-occurs with only one category of the other (is_nested()). is_cross_classified() checks if one factor level occurs in some, but not all levels of another factor.

is_crossed(f1, f2)

is_nested(f1, f2)

is_cross_classified(f1, f2)

Arguments

f1

Numeric vector or factor.

f2

Numeric vector or factor.

Value

Logical. For is_crossed(), TRUE if factors are (fully) crossed, FALSE otherwise. For is_nested(), TRUE if factors are nested, FALSE otherwise. For is_cross_classified(), TRUE, if one factor level occurs in some, but not all levels of another factor.

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)

Examples

# crossed factors, each category of
# x appears in each category of y
x <- c(1,4,3,2,3,2,1,4)
y <- c(1,1,1,2,2,1,2,2)
# show distribution
table(x, y)
#>    y
#> x   1 2
#>   1 1 1
#>   2 1 1
#>   3 1 1
#>   4 1 1
# check if crossed
is_crossed(x, y)
#> [1] TRUE

# not crossed factors
x <- c(1,4,3,2,3,2,1,4)
y <- c(1,1,1,2,1,1,2,2)
# show distribution
table(x, y)
#>    y
#> x   1 2
#>   1 1 1
#>   2 1 1
#>   3 2 0
#>   4 1 1
# check if crossed
is_crossed(x, y)
#> [1] FALSE


# 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)
#>    y
#> x   1 2 3
#>   1 1 0 0
#>   2 1 0 0
#>   3 1 0 0
#>   4 0 1 0
#>   5 0 1 0
#>   6 0 1 0
#>   7 0 0 1
#>   8 0 0 1
#>   9 0 0 1
# check if nested
is_nested(x, y)
#> 'f1' is nested within 'f2'
#> [1] TRUE
is_nested(y, x)
#> 'f2' is nested within 'f1'
#> [1] TRUE

# 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)
#>    y
#> x   1 2 3
#>   1 1 1 0
#>   2 1 0 1
#>   3 1 0 0
#>   4 0 1 0
#>   5 0 1 0
#>   6 0 1 0
#>   7 0 0 1
#>   8 0 0 1
#>   9 0 0 1
# check if nested
is_nested(x, y)
#> [1] FALSE
is_nested(y, x)
#> [1] FALSE

# also not fully crossed
is_crossed(x, y)
#> [1] FALSE

# but partially crossed
is_cross_classified(x, y)
#> [1] TRUE