简体   繁体   中英

find the ranks of elements in a vector from another longer vector in R

Suppose I have a short vector v1=c(1,3,4,7,9) and another short vector v2=c(3,4,10,12,9) . The combined "long" vector is v=c(1,3,3,4,4,7,9,9,10,12) . Now I want to find the rank of each of the elements in v1 (ie 1,3,4,7,9) in the long vector v . How can I efficiently do that in R? Thanks!

How about this?

sapply(v1,function(x) mean(which(x==v)))
# [1] 1.0 2.5 4.5 6.0 7.5

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM