Ghilborn
Ghilborn

Reputation: 29

R - Using If statement to compare factors with different levels

I would like to compare two columns in my dataset, however they have different levels. I cant seem to find a way to get this to work. Any suggestions?

Example:

x = c('a','b','c')
y = c('a','b','g')
z = data.frame(x,y)

if(z$x == z$y){1} else{0}

returns: Error in Ops.factor(z$x, z$y) : level sets of factors are different

I have tried to make them have similar levels, i.e,:

z$x <- factor(z$x, levels=c(levels(z$y),levels(z$x)))
z$y <- factor(z$y, levels=c(levels(z$y),levels(z$x)))

but it still returns the error.

ive also used is.same().

Upvotes: 2

Views: 7005

Answers (2)

akrun
akrun

Reputation: 886938

We can convert the logical to binary by using as.integer

with(z, as.integer(levels(x)[x] == levels(y)[y]))

Upvotes: 2

Sraffa
Sraffa

Reputation: 1668

You could convert them to characters for the comparison. However, if you want to compare all of the rows you'll probably want to use ifelse:

ifelse(as.character(z$x) == as.character(z$y), 1, 0)

Upvotes: 3

Related Questions