BlueFeet
BlueFeet

Reputation: 2507

R: count number of different values of 2 factor vectors

I have two vectors with the same length -- both are factors. I want to compare them position by position to see how many of are different. For example

> a = as.factor(c(1,2,2,1,2,2,1))
> b = as.factor(c(1,2,1,1,1,2,1))

Since there are two different values at position 3 and position 5, I'd like to get 2 as the final results. I know I can convert them to numeric numbers and do a subtraction, but what if the value are strings, e.g.

a = as.factor(c("a","a","b"))
b = as.factor(c("a","b","b"))

Are there any elegant, general ways to do this?

Upvotes: 0

Views: 713

Answers (2)

BlueFeet
BlueFeet

Reputation: 2507

Here's @Frank's answer:

I'd go with table(a==b)

Upvotes: 2

zero323
zero323

Reputation: 330093

If you're looking for a general solution you have to ensure that both factors have the same levels:

ab_levels <- unique(c(levels(a), levels(b)))

After that you can simply sum:

sum(factor(a, ab_levels) == factor(b, ab_levels))

Without adjusting levels both table + == and sum will fail on something like this:

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

Another approach is to use table without ==:

> tbl <- table(a, b)
> sum(tbl) - sum(diag(tbl))

Upvotes: 5

Related Questions