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 ###################### Thanks, Sachin [[alternative HTML version deleted]]
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
I suggest using vectorization : find_row <- function(m,v) { which(!(abs(rowSums(m - rep(v, each = nrow(m))) )) > 0) } The function matroweqv mentioned above would give any row with the first element equal to the first element in vector v. The function find_row matches each row of the matrix as a whole to the vector v. 2013/6/26 Sachinthaka Abeywardana <sachin.abeywardana@gmail.com>> 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 > ###################### > > Thanks, > Sachin > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@r-project.org 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. >-- Sincerely yours, Yulia Matveyeva, Department of Statistical Modelling, Faculty of Mathematics and Mechanics, St Petersburg State University, Russia [[alternative HTML version deleted]]
Hi, Try: ?roweqv<- function(m,v) which(!is.na(match(interaction(as.data.frame(m),drop=TRUE),paste(v,collapse=".")))) v<- c(2,5,8) roweqv(m,v) #[1] 2 set.seed(24) m1<-matrix(sample(1:15,3e5,replace=TRUE),ncol=3) v1<- c(10,12,4) ?system.time(res<- roweqv(m1,v1)) ? # user? system elapsed ? #0.132?? 0.000?? 0.130 res # [1]???? 5?? 381? 2760? 3793? 9667 16881 18866 21219 24961 36220 38366 54382 #[13] 54951 55825 57167 67636 70713 71087 73284 82797 83255 85748 86216 86690 #[25] 93120 95399 96370 head(m1[res,]) #???? [,1] [,2] [,3] #[1,]?? 10?? 12??? 4 #[2,]?? 10?? 12??? 4 #[3,]?? 10?? 12??? 4 #[4,]?? 10?? 12??? 4 #[5,]?? 10?? 12??? 4 #[6,]?? 10?? 12??? 4 v2<- c(20,5,4) roweqv(m1,v2) #integer(0) A.K. ----- Original Message ----- From: Sachinthaka Abeywardana <sachin.abeywardana at gmail.com> To: "r-help at r-project.org" <r-help at r-project.org> Cc: Sent: Wednesday, June 26, 2013 4:03 AM Subject: [R] match rows of R 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 ###################### Thanks, Sachin ??? [[alternative HTML version deleted]] ______________________________________________ R-help at r-project.org 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.