[R] Matching rows?
gb
gb at stat.umu.se
Mon Jul 3 14:37:34 CEST 2000
On Mon, 3 Jul 2000, Bill Venables wrote:
> At 01:15 PM 03/07/00 +0200, gb wrote:
> >
> >I have a matrix X with many rows and a vector y, which I want
> >to match against the rows of the matrix. I especially want
> >to know if there is a match or not. I can do this with 'all'
> >and a 'while' construct:
> >
> >row.match <- function(y, X)
> >{
> > found <- FALSE
> > j <- 0
> > while ( (!found) && (j < nrow(X)) )
> > {
> > j <- j + 1
> > found <- all(y == X[j, ])
> > }
> > return ( found )
> >}
> >
> >Two alternatives:
> >
> >any( apply(X, 1, all.equal, y) == "TRUE")
> >
> >any(apply(apply(X, 1, "==", y), 1, all))
> >
>
> Here is a third alternative. It sets out to find which rows match the
> vector rather than merely if there is a match. It returns a vector of row
> indices of the matching rows, which is empty if there are no matches, of
> course:
>
> > row.matches <- function(y, X) {
> i <- seq(nrow(X))
> j <- 0
> while(length(i) && (j <- j + 1) < ncol(X))
> i <- i[X[i, j] == y[j]]
> i
> }
>
> Here is a quick checkout
>
> > X <- matrix(sample(0:1, 53000*11, rep=T), 53000, 11)
> > y <- X[53000, ]
> > dos.time(row.match(y, X))
> [1] 1.48
> > dos.time(row.matches(y, X))
> [1] 0.16003
[...]
Amazing, and smart! "Looping" over a few columns istead of over
many rows does the trick, I guess. I got even bigger differences
on my machine.
One tiny error, though: The "while" statement should read
while(length(i) && (j <- j + 1) <= ncol(X))
^
I think. Thanks! |
Göran
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
More information about the R-help
mailing list