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
Sorry about that I realized that I posted code that doesnt work ... below
y should be replaced by the parameter m in the body of the function.
(I tried to clean it up to post, but that never works...)
has.row <- function(m, r) {
for (i in 1:length(m[,1])) {
x <- ifelse(m[i,1:3] == r, 1, 0)
if (sum(x) == 3) {
return(TRUE)
}
}
return(FALSE)
}
thanks. Jim
James Bullard wrote:
> 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
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
>
--
James Bullard
bullard at berkeley.edu
760.267.0986
Try something like:> x1 <- matrix(sample(1:5, 30, replace=TRUE), ncol=3) > x2 <- x1[4,] > which(colSums(abs(t(x1) - x2)) == 0)[1] 4 Note: If the data are not all integers, you probably should test whether the absolute sum differences is less than some very small number, rather than == 0. This uses the recyling rule, which, if you don't know about, probably would be good to find out. Andy> From: James Bullard > > 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 > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > >