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

Gabor Grothendieck ggrothendieck at myway.com
Mon Jul 19 21:41:03 CEST 2004


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




More information about the R-help mailing list