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

Kjetil Halvorsen kjetil at acelerate.com
Tue Jul 20 00:13:31 CEST 2004


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 at 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
>
>
>  
>




More information about the R-help mailing list