[R] Equality of factor levels problem

Martín Cañón martincmd at hotmail.com
Tue Jan 5 16:58:39 CET 2016


Hi to all useRs on the list.

I want to know if there is a function in R which evaluates if observations of two factors (similar but with unequal levels) in a data frame are equal at row level.

Example of dataset:

var1 <- c(1, 2, 3, 2, 1)
var2 <- c(1, 3, 3, 4, 2)
dat <- data.frame(var1, var2)
dat$var1 <- factor(dat$var1, labels = c("low", "medium", "high"))
dat$var2 <- factor(dat$var2, labels = c("low", "medium", "high", "very high"))

What I want as a result is a logical vector that says if observation [i, 1] is equal to observation [i, 2].

Something like...

var1[1] == var2[1]  # TRUE
var1[2] == var2[2]  # FALSE
var1[3] == var2[3]  # TRUE
var1[4] == var2[4]  # FALSE
var1[4] == var2[4]  # FALSE

I've tried equal signs (==), commands such as "identical" and "all.equal" in "for loops" and I can't make it work for factors. 

It seems very simple but I just don't find the way to do it.

Regards,

Martín 

 		 	   		  
	[[alternative HTML version deleted]]



More information about the R-help mailing list