[R] Matching rows?
Bill Venables
Bill.Venables at cmis.csiro.au
Mon Jul 3 14:00:20 CEST 2000
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
To check if a match exists:
> length(row.matches(y, X)) > 0
but you seem to be throwing away potentially useful information.
Bill Venables.
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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