Hello, I'm looking for a fast way to retrieve the position of elements from a vector which match element in another vector. Example va<-c('a','e') vb<-c('a','b','c','d','e','f','e') f(va,vb) should return c(1,5,7) I have 2 different cases: I) my vector vb contains only distinct values II) my vector vb may contains the same values more than once. for I) I use following which is fast enough: as.vector(na.omit(unlist(lapply(va,match,vb)))) Thanks for any help, Marc
Something like:> which(vb %in% va)[1] 1 5 7 should handle both cases, I believe. Andy> From: Marc Mamin > > Hello, > > I'm looking for a fast way to retrieve the position of > elements from a vector which match element in another vector. > > Example > > va<-c('a','e') > vb<-c('a','b','c','d','e','f','e') > > f(va,vb) should return c(1,5,7) > > I have 2 different cases: > > I) my vector vb contains only distinct values > II) my vector vb may contains the same values more than once. > > for I) I use following which is fast enough: > > as.vector(na.omit(unlist(lapply(va,match,vb)))) > > Thanks for any help, > > Marc > > ______________________________________________ > 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 > >
Marc Mamin wrote:> Hello, > > I'm looking for a fast way to retrieve the position of elements from a vector which match element in another vector. > > Example > > va<-c('a','e') > vb<-c('a','b','c','d','e','f','e') > > f(va,vb) should return c(1,5,7) > > I have 2 different cases: > > I) my vector vb contains only distinct values > II) my vector vb may contains the same values more than once. > > for I) I use following which is fast enough: > > as.vector(na.omit(unlist(lapply(va,match,vb))))which(vb %in% va) Uwe Ligges> Thanks for any help, > > Marc > > ______________________________________________ > 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
"Marc Mamin" <M.Mamin at intershop.de> writes:> Hello, > > I'm looking for a fast way to retrieve the position of elements from a vector which match element in another vector. > > Example > > va<-c('a','e') > vb<-c('a','b','c','d','e','f','e') > > f(va,vb) should return c(1,5,7) > > I have 2 different cases: > > I) my vector vb contains only distinct values > II) my vector vb may contains the same values more than once. > > for I) I use following which is fast enough: > > as.vector(na.omit(unlist(lapply(va,match,vb))))Wouldn't know about speed, but which(apply(outer(va,vb,"=="),2,any)) which(sapply(vb,"%in%",va)) both seem to do the trick. -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
Marc Mamin wrote:> Hello, > > I'm looking for a fast way to retrieve the position of elements from a vector which match element in another vector. > > Example > > va<-c('a','e') > vb<-c('a','b','c','d','e','f','e') > > f(va,vb) should return c(1,5,7) > > I have 2 different cases: > > I) my vector vb contains only distinct values > II) my vector vb may contains the same values more than once. > > for I) I use following which is fast enough: > > as.vector(na.omit(unlist(lapply(va,match,vb)))) > > Thanks for any help, > > Marc > > ______________________________________________ > 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 >Hi Marc, How about this? which(vb %in% va) Let me know if you have any more questions. Kevin