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

From: Jim Brennan <jfbrennan_at_rogers.com>
Date: Tue 20 Jul 2004 - 11:52:55 EST

Yep it is faster but it is wrong! :-) Maybe good for two columns only... ----- Original Message -----
From: "Liaw, Andy" <andy_liaw@merck.com> To: "'Jim Brennan'" <jfbrennan@rogers.com>; <r-help@stat.math.ethz.ch> Sent: Monday, July 19, 2004 9:45 PM
Subject: RE: [R] How to compare X1 = X2 = ... = Xn?

> Not so fast:
>
> > x <- matrix(rep(c(2, 1, 3), 2), nr=2, byrow=TRUE)
> > x
> [,1] [,2] [,3]
> [1,] 2 1 3
> [2,] 2 1 3
> > rowSums(x) / ncol(x) == x[,1]
> [1] TRUE TRUE
>
> Andy
>
> > From: Jim Brennan
> >
> > This similar method may be quicker
> > x1\$new <- 1*(rowSums(x1)/ncol(x1)==x1[,1])
> > Learning lots from these type questions!
> >
> > Jim
> >
> > From: "Gabor Grothendieck"
> >
> > > Berton Gunter <gunter.berton <at> gene.com> writes:
> > >
> > > :
> > > : How about:
> > > :
> > > : X<-as.matrix(yourframe)
> > > : apply(X,2, '==',X[,1])%*%rep(1,ncol(X)) == ncol(x)
> > > :
> > > : avoiding the rowwise apply overhead?
> > >
> > > Following up on your idea we can use rowSums instead of matrix
> > multiplication
> > > to speed it up even more:
> > >
> > > R> x <- data.frame(X1 = c(1.5, 1.5, 2.5, 4.5),
> > > + X2 = c(4.5, 1.5, 2.5, 5.5), X3 = c(2.5, 1.5, 2.5, 2.5))
> > > R> set.seed(1)
> > > R> x1 <- x2 <- x[sample(4,100000,rep=T),]
> > >
> > > R> gc();system.time({x1\$new <- (rowSums(x1==x1[,1])==ncol(x))+0})
> > > used (Mb) gc trigger (Mb)
> > > Ncells 634654 17.0 1590760 42.5
> > > Vcells 1017322 7.8 3820120 29.2
> > > [1] 0.48 0.00 0.48 NA NA
> > >
> > > R> gc(); system.time({X <- as.matrix(x2); x2\$new <- c(apply(X,2,
> > '==',X[,1])%*%
> > > rep(1,ncol(X)) == ncol(x))+0})
> > > used (Mb) gc trigger (Mb)
> > > Ncells 634668 17.0 1590760 42.5
> > > Vcells 1517333 11.6 3820120 29.2
> > > [1] 1.39 0.03 1.50 NA NA
> > >
> > > R> all.equal(x1,x2)
> > > [1] TRUE
> > >
> > > ______________________________________________
> > > R-help@stat.math.ethz.ch mailing list
> > > https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> > 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
> > http://www.R-project.org/posting-guide.html
> >
> >
>
>
> --------------------------------------------------------------------------

> Notice: This e-mail message, together with any attachments, contains
information of Merck & Co., Inc. (One Merck Drive, Whitehouse Station, New Jersey, USA 08889), and/or its affiliates (which may be known outside the United States as Merck Frosst, Merck Sharp & Dohme or MSD and in Japan, as Banyu) that may be confidential, proprietary copyrighted and/or legally privileged. It is intended solely for the use of the individual or entity named on this message. If you are not the intended recipient, and have received this message in error, please notify us immediately by reply e-mail and then delete it from your system.
> --------------------------------------------------------------------------

R-help@stat.math.ethz.ch mailing list