Avi
Avi

Reputation: 2283

Adding maximal levels to both factors

I have 2 factors A and B: Factor A has the following levels:

> levels (A)
[1] "1" "2" "3"

Whereas factor B has the following levels:

> levels (B)
[1] "1" "2"

I would like to calculate the proportion of elements for which their levels coincide, so I use the following:

C<-mean (A == B)

Since they have to be with same level. I added to B the "3" level by:

levels(B) <- c(levels(B), "3")

However it is just an example, since sometimes A has more levels than B and sometimes B has more levels than A. I would like to insert to each of them the maximal number of levels so they will be the same and I'll be able to calculate C. How can I do it?

Upvotes: 2

Views: 50

Answers (1)

josliber
josliber

Reputation: 44330

It sounds like you're trying to check the proportion of times when the levels of two factors coincide and running into errors when the factors have different level sets; perhaps something like:

a <- factor(c("a", "b", "c"))
b <- factor(c("b", "b", "c"))
a == b
# Error in Ops.factor(a, b) : level sets of factors are different

Instead of messing with the levels of the factors to get rid of this error, I would simply use as.character to grab the names of the levels and then compare:

mean(as.character(a) == as.character(b))
# [1] 0.6666667

Upvotes: 6

Related Questions