You want to compare the values of the levels, I think?
What about:> as.character(dat$var1) == as.character(dat$var2)
[1] TRUE FALSE TRUE FALSE FALSE
Sarah
On Tue, Jan 5, 2016 at 10:58 AM, Mart?n Ca??n <martincmd at hotmail.com>
wrote:> 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
>
--
Sarah Goslee
http://www.numberwright.com