[R] How to compare X1 = X2 = ... = Xn?

Sundar Dorai-Raj sundar.dorai-raj at PDF.COM
Mon Jul 19 20:30:49 CEST 2004


Liaw, Andy wrote:

> Here's an alternative:
> 
> 
>>x <- data.frame(X1 = c(1, 1, 2, 4),
> 
> +                  X2 = c(4, 1, 2, 5),
> +                  X3 = c(2, 1, 2, 2))
> 
>>check <- paste(names(x), collapse=" == ")
>>with(x, eval(parse(text=check)))
> 
> [1] FALSE  TRUE FALSE FALSE

Oops. Should be

 > [1] FALSE  TRUE  TRUE FALSE

This is TRUE for the second case by accident since the second element is 1.

 > x$X4 <- (x$X1 == x$X2)
 > as.numeric(x$X4)
[1] 0 1 1 0
 > x$X4 == x$X3
[1] FALSE  TRUE FALSE FALSE
 >




More information about the R-help mailing list