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

From: Robin Hankin <rksh_at_soc.soton.ac.uk>
Date: Tue 20 Jul 2004 - 18:12:54 EST

just one more that seems natural but hasn't been mentioned:

library(magic)
x <- data.frame(a=c(1,2,1,2,1),b=c(1,2,2,1,2),c=c(1,2,1,2,1)) apply(x,1,minmax)

     1 2 3 4 5
  TRUE TRUE FALSE FALSE FALSE
>

[
here
  minmax <-
function (x)
{

     max(x) == min(x)
}

]

rksh

>How about:
>
>
>X<-as.matrix(yourframe)
>apply(X,2, '==',X[,1])%*%rep(1,ncol(X)) == ncol(x)
>
>avoiding the rowwise apply overhead?
>
>Cheers,
>
>Bert Gunter
>
>Non-Clinical Biostatistics
>Genentech
>MS: 240B
>Phone: 650-467-7374
>
>
>"The business of the statistician is to catalyze the scientific learning
>process."
>
> -- George E.P. Box
>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
>

-- 
Robin Hankin
Uncertainty Analyst
Southampton Oceanography Centre
SO14 3ZH
tel +44(0)23-8059-7743
initialDOTsurname@soc.soton.ac.uk (edit in obvious way; spam precaution)

______________________________________________
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 18:22:23 2004

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