Question

I have a data.frame as below:

set.seed(2013)
df <- data.frame(site = sample(c("A","B","C"), 10, replace = TRUE),
                 currency = sample(c("USD", "EUR", "GBP", "CNY", "CHF"),10, replace=TRUE, prob=c(10,6,5,6,0.5)),
                 value = sample(seq(1:10)/10,10,replace=FALSE))

I want to create a rank column that lists the order of a corresponding row with respect to its value. I tried sort() and order() but these return different things. How can I get my desired output listed under the "rank" column?

df$order <- order(df$value)
df$sort <- sort(df$value)
df

  site   currency  value   order  sort    | rank
1   B       USD     0.6      2     0.1    |  6
2   C       USD     0.1      5     0.2    |  1
3   C       CNY     0.9      6     0.3    |  9
4   C       GBP     1.0      10    0.4    |  10
5   A       CNY     0.2      8     0.5    |  2
6   C       CNY     0.3      1     0.6    |  3
7   C       GBP     0.8      9     0.7    |  8
8   C       GBP     0.5      7     0.8    |  5
9   C       USD     0.7      3     0.9    |  7
10  C       USD     0.4      4     1.0    |  4
Was it helpful?

Solution

You are looking for rank()...

 rank( df$value )
 # [1]  6  1  9 10  2  3  8  5  7  4

order() gives the ordered indices of the vector you are ordering. So, if you do order( df$value ) you get [1] 2 5 6 10 8 1 9 7 3 4 because the 2nd value of df$value should go first, the fifth value should be 2nd and so on. Ordinarily you use it to order a vector or data.frame according to some atomic vector like so...

df[ order( df$value ) , ]
#   site currency value
#2     C      USD   0.1
#5     A      CNY   0.2
#6     C      CNY   0.3
#10    C      USD   0.4
#8     C      GBP   0.5
#1     B      USD   0.6
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top