Search code examples
rvectorrankingrank

r rank value in vector


I have a sequence of numbers and vectors that should be used to evaluate each of the sequence of numbers according to their ranks.

Example

 a <- c(20, 29, 22)
 b <- c(21, 22, 27)
 data <- data.frame(cbind(a,b))
 c1 <- c(12,23,34,21,33,22,35,3,5,6,7,29,49)
 c2 <- c(12,23,34,21,33,22,35,3,5,6,7,29,49)

How can I check the rank of each data$a value in vector c1 and each data$b value in c2? Any ideas? (I abstracted the problem, but there are in sum 28 values for each individual (a,b,...) that I would need to rank according to 28 values of c (c1, c2, ..., c28)).

For the first value of a = 20, there would be 6 needed, since of the elements of c1; 12, 3, 5, 6, and 7 are all smaller.

Any ideas?


Solution

  • what about creating a new vector with the value appended and rank that ? so creating a loop that for each element of vectora does:

    rankVector=rank(append(c(a),c1))
    

    And then retrieve the first value of rankVector.

    The full loop would look like:

    for (i in seq_along(a) ){
    
       rankVector[i]=rank(append(c(a[i]),c1))
    
    }
    

    Cheers !