Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 14th, 2012  |  syntax: None  |  size: 0.52 KB  |  hits: 19  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How to select rows which are not unique in columns of matrix in R
  2. x[apply(x, 1, sd) > 0, ]
  3.        
  4. x <- matrix(sample(3:5,30000,T), ncol=3)    
  5.  
  6. system.time(x2 <- x[apply(x,1,sd) > 0, ])
  7. user  system elapsed
  8. 0.960   0.000   0.961
  9.  
  10. system.time(x2 <- x[apply(x,1,FUN=function(r){return(length(unique(r)))}) > 1,])
  11. user  system elapsed
  12. 0.470   0.000   0.465
  13.        
  14. system.time(x2 <- x[rowSums(abs(x - rowMeans(x))) != 0, ])
  15. user  system elapsed
  16. 0.000   0.000   0.001
  17.        
  18. x[apply(x,1,FUN=function(r){return(length(unique(r)))}) > 1,]