Reputation: 285
I have a question regarding factors in R. Is there a way to compare the levels of each factor? I am interested in whether a level of one factor is a subset of a level of another factor. For example, let's say we have a vector:
a <- c(1,1,2,2,3,3,4,4,4)
a1 <- cut(a, breaks=c(1,2,3,4), include.lowest=TRUE)
a2 <- cut(a, breaks=c(1,3,4), include.lowest=TRUE)
levels(a1)
[1] "[1,2]" "(2,3]" "(3,4]"
levels(a2)
"[1,3]" "(3,4]"
So the first level of a2
includes the first two levels of a1
. I need to know all such relations in some data. Is there a function in R which would give me all such combinations? Or at least a way to manually compare levels of a factor (extract breaks maybe), so I could write a function that does that? For now I've only done it with comparing the names of the levels, but that's not really efficient, since I usually don't have labels set at intervals.
Upvotes: 0
Views: 2020
Reputation: 24535
I think you are looking for table function:
> table(a1, a2)
a2
a1 [1,3] (3,4]
[1,2] 4 0
(2,3] 2 0
(3,4] 0 3
Upvotes: 1