
Untitled
By: a guest on
Jun 14th, 2012 | syntax:
None | size: 0.52 KB | hits: 19 | expires: Never
How to select rows which are not unique in columns of matrix in R
x[apply(x, 1, sd) > 0, ]
x <- matrix(sample(3:5,30000,T), ncol=3)
system.time(x2 <- x[apply(x,1,sd) > 0, ])
user system elapsed
0.960 0.000 0.961
system.time(x2 <- x[apply(x,1,FUN=function(r){return(length(unique(r)))}) > 1,])
user system elapsed
0.470 0.000 0.465
system.time(x2 <- x[rowSums(abs(x - rowMeans(x))) != 0, ])
user system elapsed
0.000 0.000 0.001
x[apply(x,1,FUN=function(r){return(length(unique(r)))}) > 1,]