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

From: Kjetil Halvorsen <kjetil_at_acelerate.com>
Date: Tue 20 Jul 2004 - 08:13:31 EST

This is'nt very elegant, and you may want to replace identical () with a function doing more appropiate numerical floating-point comparison:

 > midentical <-
function(...){

   dots <- list(...)
   n <- length(dots)
   ans <- TRUE
   for ( i in (1:(n-1))) {

      ans <- ans && identical(dots[i], dots[i+1])    }
   return(ans)
}

 > x1 <- c(1,2,3,4,5)
 > x2 <- c(1,3,2,4,5)
 > x3 <- c(1,6,7,4,5)
 > mapply(function(...) ifelse(midentical(...),1,0),x1,x2,x3)
[1] 1 0 0 1 1

Kjetil Halvorsen

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
>
>
> [[alternative HTML version deleted]]
>
>______________________________________________
>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 13:10:35 2004

This archive was generated by hypermail 2.1.8 : Wed 03 Nov 2004 - 22:55:05 EST