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 > > > > ______________________________________________ > > [EMAIL PROTECTED] 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 > > ______________________________________________ > [EMAIL PROTECTED] 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 > > ______________________________________________ [EMAIL PROTECTED] 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