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

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

This similar method may be quicker
 x1$new <- 1*(rowSums(x1)/ncol(x1)==x1[,1]) Learning lots from these type questions!

Jim
----- Original Message -----
From: "Gabor Grothendieck" <ggrothendieck@myway.com> To: <r-help@stat.math.ethz.ch>
Sent: Monday, July 19, 2004 3:41 PM
Subject: Re: [R] How to compare X1 = X2 = ... = Xn?

> 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)
uotelev1">> [1] TRUE
>
> ______________________________________________
> 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 11:33:37 2004

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