[R] row equality.

James Bullard bullard at berkeley.edu
Sun Feb 13 23:55:57 CET 2005


I think that this is an easy one...

I have a matrix where each row is an (x,y,z) triplet. Given a potential 
(xnew,ynew,znew) triplet I want to know if the matrix already contains a 
row with the new values (the space already has that point). I can do it 
using a for loop, but I would like to know if there is anyway in which I 
can do it without the for loop.

I do it now like this (this algorithm appears to be correct, but there 
are probably much cleaner ways to write it.)

has.row <- function(m, r) {
  for (i in 1:length(y[,1])) {
    x <- ifelse(y[i,1:3] == r, 1, 0)
    if (sum(x) == 3) {
      return(TRUE)
    }
  }
  return(FALSE)
}



Thanks, jim




More information about the R-help mailing list