[R] match rows of R

Berend Hasselman bhh at xs4all.nl
Wed Jun 26 10:17:40 CEST 2013


On 26-06-2013, at 10:03, Sachinthaka Abeywardana <sachin.abeywardana at gmail.com> wrote:

> Hi all,
> 
> What would be an efficient way to match rows of a matrix to a vector?
> 
> ex:
> 
> m<-matrix(1:9, nrow=3)
> 
> m     [,1] [,2] [,3]
> [1,]    1    4    7
> [2,]    2    5    8
> [3,]    3    6    9
> 
> #################################
> which(m==c(2,5,8))        # I want this to return 2
> ######################

Something like this:

matroweqv <- function(m,v) which(t(m)==v, arr.ind=TRUE)[,2][1]

matroweqv(m,c(2,5,8))
# [1] 2


Berend



More information about the R-help mailing list