Return "TRUE" only for first match of values between matrix and vector.

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Return "TRUE" only for first match of values between matrix and vector.

Nevil Amos
I wish to return " True" in a matrix for only the first match of a value
per row where the value equals that in a vector with the same number of
values as rosw in the matrix

eg:
A<-matrix(c(2,3,2,1,1,2,NA,NA,NA,5,1,0,5,5,5),5,3)
B<-c(2,1,NA,1,5)
desired result:

      [,1] [,2]  [,3]
[1,]  TRUE FALSE FALSE
[2,] FALSE   NA FALSE
[3,]    NA   NA    NA
[4,]  TRUE   NA FALSE
[5,] FALSE TRUE  FALSE

however A==B returns:
      [,1] [,2]  [,3]
[1,]  TRUE TRUE FALSE
[2,] FALSE   NA FALSE
[3,]    NA   NA    NA
[4,]  TRUE   NA FALSE
[5,] FALSE TRUE  TRUE
and
apply(A,1,function(x) match (B,x))
returns
     [,1] [,2] [,3] [,4] [,5]
[1,]    1   NA    1   NA   NA
[2,]    3   NA   NA    1    1
[3,]   NA    2    2    2   NA
[4,]    3   NA   NA    1    1
[5,]   NA   NA    3    3    2

thanks

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Return "TRUE" only for first match of values between matrix and vector.

arun kirshna
Hi,
Try:
indx <- A==B
t(apply(indx,1,function(x) {x[duplicated(x) & !is.na(x)] <- FALSE; x}))
#      [,1]  [,2]  [,3]
#[1,]  TRUE FALSE FALSE
#[2,] FALSE    NA FALSE
#[3,]    NA    NA    NA
#[4,]  TRUE    NA FALSE
#[5,] FALSE  TRUE FALSE

A.K.




On Friday, May 2, 2014 4:47 AM, nevil amos <[hidden email]> wrote:
I wish to return " True" in a matrix for only the first match of a value
per row where the value equals that in a vector with the same number of
values as rosw in the matrix

eg:
A<-matrix(c(2,3,2,1,1,2,NA,NA,NA,5,1,0,5,5,5),5,3)
B<-c(2,1,NA,1,5)
desired result:

      [,1] [,2]  [,3]
[1,]  TRUE FALSE FALSE
[2,] FALSE   NA FALSE
[3,]    NA   NA    NA
[4,]  TRUE   NA FALSE
[5,] FALSE TRUE  FALSE

however A==B returns:
      [,1] [,2]  [,3]
[1,]  TRUE TRUE FALSE
[2,] FALSE   NA FALSE
[3,]    NA   NA    NA
[4,]  TRUE   NA FALSE
[5,] FALSE TRUE  TRUE
and
apply(A,1,function(x) match (B,x))
returns
     [,1] [,2] [,3] [,4] [,5]
[1,]    1   NA    1   NA   NA
[2,]    3   NA   NA    1    1
[3,]   NA    2    2    2   NA
[4,]    3   NA   NA    1    1
[5,]   NA   NA    3    3    2

thanks

    [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Return "TRUE" only for first match of values between matrix and vector.

Jorge I Velez
In reply to this post by Nevil Amos
Hi Nevil,

Try

apply(A, 2, function(x) x == B)

HTH,
Jorge.-



On Fri, May 2, 2014 at 6:46 PM, nevil amos <[hidden email]> wrote:

> I wish to return " True" in a matrix for only the first match of a value
> per row where the value equals that in a vector with the same number of
> values as rosw in the matrix
>
> eg:
> A<-matrix(c(2,3,2,1,1,2,NA,NA,NA,5,1,0,5,5,5),5,3)
> B<-c(2,1,NA,1,5)
> desired result:
>
>       [,1] [,2]  [,3]
> [1,]  TRUE FALSE FALSE
> [2,] FALSE   NA FALSE
> [3,]    NA   NA    NA
> [4,]  TRUE   NA FALSE
> [5,] FALSE TRUE  FALSE
>
> however A==B returns:
>       [,1] [,2]  [,3]
> [1,]  TRUE TRUE FALSE
> [2,] FALSE   NA FALSE
> [3,]    NA   NA    NA
> [4,]  TRUE   NA FALSE
> [5,] FALSE TRUE  TRUE
> and
> apply(A,1,function(x) match (B,x))
> returns
>      [,1] [,2] [,3] [,4] [,5]
> [1,]    1   NA    1   NA   NA
> [2,]    3   NA   NA    1    1
> [3,]   NA    2    2    2   NA
> [4,]    3   NA   NA    1    1
> [5,]   NA   NA    3    3    2
>
> thanks
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Return "TRUE" only for first match of values between matrix and vector.

arun kirshna
In reply to this post by arun kirshna
Hi,
This should be little more faster.


indx <- A==B
indx1 <- which(indx, arr.ind=TRUE)
indx[indx1[duplicated(indx1[,1]),]]<- FALSE
indx


##Speed comparison
##previous method
fun1 <- function(mat, vec) {
    stopifnot(dim(mat)[1] == length(vec))
    indx <- mat == vec
    t(apply(indx, 1, function(x) {
        x[duplicated(x) & !is.na(x)] <- FALSE
        x
    }))
}

##modified
fun2 <- function(mat, vec) {
    stopifnot(dim(mat)[1] == length(vec))
    indx <- mat == vec
    indx1 <- which(indx, arr.ind = TRUE)
    indx[indx1[duplicated(indx1[, 1]), ]] <- FALSE
    indx
}
identical(fun1(A,B), fun2(A,B))
#[1] TRUE
set.seed(498)
A1 <- matrix(sample(40,1e5*500,replace=TRUE), ncol=500)
set.seed(345)
 B1 <- sample(70, 1e5, replace=TRUE)

system.time(res1 <- fun1(A1,B1))
 # user  system elapsed
 # 7.840   0.344   8.195

system.time(res2 <- fun2(A1,B1))
#  user  system elapsed
#  0.304   0.080   0.382
identical(res1,res2)
#[1] TRUE
which(rowSums(res1,na.rm=TRUE)>1)
#integer(0)

A.K.


On Friday, May 2, 2014 7:51 AM, arun <[hidden email]> wrote:
Hi,
Try:
indx <- A==B
t(apply(indx,1,function(x) {x[duplicated(x) & !is.na(x)] <- FALSE; x}))
#      [,1]  [,2]  [,3]
#[1,]  TRUE FALSE FALSE
#[2,] FALSE    NA FALSE
#[3,]    NA    NA    NA
#[4,]  TRUE    NA FALSE
#[5,] FALSE  TRUE FALSE

A.K.





On Friday, May 2, 2014 4:47 AM, nevil amos <[hidden email]> wrote:
I wish to return " True" in a matrix for only the first match of a value
per row where the value equals that in a vector with the same number of
values as rosw in the matrix

eg:
A<-matrix(c(2,3,2,1,1,2,NA,NA,NA,5,1,0,5,5,5),5,3)
B<-c(2,1,NA,1,5)
desired result:

      [,1] [,2]  [,3]
[1,]  TRUE FALSE FALSE
[2,] FALSE   NA FALSE
[3,]    NA   NA    NA
[4,]  TRUE   NA FALSE
[5,] FALSE TRUE  FALSE

however A==B returns:
      [,1] [,2]  [,3]
[1,]  TRUE TRUE FALSE
[2,] FALSE   NA FALSE
[3,]    NA   NA    NA
[4,]  TRUE   NA FALSE
[5,] FALSE TRUE  TRUE
and
apply(A,1,function(x) match (B,x))
returns
     [,1] [,2] [,3] [,4] [,5]
[1,]    1   NA    1   NA   NA
[2,]    3   NA   NA    1    1
[3,]   NA    2    2    2   NA
[4,]    3   NA   NA    1    1
[5,]   NA   NA    3    3    2

thanks

    [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.