The code below is working, but the rank is a little weird. See that there are several 21.5
. But I would like it to have an order of 1,2,3,4,5
and so on. Can you help me?
df1<-structure(list(Distance = c(5248.99947051215, 64018.2500397613,
63884.2473586514, 63822.8276312568, 63797.0409132363, 63797.0409132363,
63794.4869445083, 63792.0363232768, 63792.0363232768, 63792.0363232768,
63792.0363232768, 63766.907179171, 63763.0472418499, 63772.2020429773,
63772.2020429773, 63772.2020429773, 63771.3399204139, 63766.8444809863,
63766.8444809863, 63760.4694766072, 63760.4694766072, 63760.4694766072,
63760.4694766072, 63760.4694766072, 63760.4694766072, 63760.4694766072,
63759.7505492485, 63758.58565, 63758.58565)), class = "data.frame", row.names = c("1",
"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13",
"14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24",
"25", "26", "27", "28", "29"))
df1$Rank <- rank(df1$Distance)
> df1
Distance Rank
1 5248.999 1.0
2 64018.250 29.0
3 63884.247 28.0
4 63822.828 27.0
5 63797.041 25.5
6 63797.041 25.5
7 63794.487 24.0
8 63792.036 21.5
9 63792.036 21.5
10 63792.036 21.5
11 63792.036 21.5
12 63766.907 15.0
13 63763.047 12.0
14 63772.202 18.0
15 63772.202 18.0
16 63772.202 18.0
17 63771.340 16.0
18 63766.844 13.5
19 63766.844 13.5
20 63760.469 8.0
21 63760.469 8.0
22 63760.469 8.0
23 63760.469 8.0
24 63760.469 8.0
25 63760.469 8.0
26 63760.469 8.0
27 63759.751 4.0
28 63758.586 2.5
29 63758.586 2.5
CodePudding user response:
rank()
by default will set ties to be an average. Try setting the ties to the appropriate value for your situation:
## increasing values at each index set of ties
df1$Rank <- rank(df1$Distance, ties.method = "first")
# [1] 1 29 28 27 25 26 24 20 21 22 23 15 12 17 18 19 16 13 14 5 6 7 8 9 10 11 4 2 3
## decreasing values at each index set of ties
df1$Rank <- rank(df1$Distance, ties.method = "last")
#[1] 1 29 28 27 26 25 24 23 22 21 20 15 12 19 18 17 16 14 13 11 10 9 8 7 6 5 4 3 2
You can also set ties to min
, max
, first
, last
and random
If you want the data frame to be ordered by rank, just add:
df.sort <- df1[order(df1$Rank),]
> df.sort$Rank
# [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #21 22 23 24 25 26 27 28 29