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

From: Liaw, Andy <andy_liaw_at_merck.com>
Date: Tue 20 Jul 2004 - 04:13:52 EST


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 Cheers,
Andy

> From: Sundar Dorai-Raj
>
> F Duan wrote:
>
> > Dear All,
> >
> > I have a data frame with n columns: X1, X2, ., Xn. Now I
> want to create a
> > new column: if X1 = X2 = . = Xn, the value is 1; Otherwise,
> the value is 0.
> >
> > How to do that in a quick way instead of doing (n choose 2)
> comparisons?
> >
> > Thank you,
> >
> > Frank
>
> How about something like?
>
> x <- data.frame(X1 = c(1, 1, 2, 4),
> X2 = c(4, 1, 2, 5),
> X3 = c(2, 1, 2, 2))
> nuniq <- function(x) length(unique(x))
> as.numeric(apply(as.matrix(x), 1, nuniq) == 1)
>
> --sundar
>
> ______________________________________________
> R-help@stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
>
>



R-help@stat.math.ethz.ch mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Tue Jul 20 04:31:59 2004

This archive was generated by hypermail 2.1.8 : Fri 18 Mar 2005 - 02:36:52 EST