# [R] Equality of factor levels problem

Sarah Goslee sarah.goslee at gmail.com
Tue Jan 5 17:39:18 CET 2016

```You want to compare the values of the levels, I think?

> as.character(dat\$var1) == as.character(dat\$var2)
  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 == var2  # TRUE
> var1 == var2  # FALSE
> var1 == var2  # TRUE
> var1 == var2  # FALSE
> var1 == var2  # 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

```