rank and order in R
set.seed(1)
x <- sample(1:50, 30)
x
# [1] 14 19 28 43 10 41 42 29 27 3 9 7 44 15 48 18 25 33 13 34 47 39 49 4 30 46 1 40 20 8
rank(x)
# [1] 9 12 16 25 7 23 24 17 15 2 6 4 26 10 29 11 14 19 8 20 28 21 30 3 18 27 1 22 13 5
order(x)
# [1] 27 10 24 12 30 11 5 19 1 14 16 2 29 17 9 3 8 25 18 20 22 28 6 7 4 13 26 21 15 23
rank
returns a vector with the "rank" of each value. the number in the first position is the 9th lowest. order
returns the indices that would put the initial vector x
in order.
The 27th value of x
is the lowest, so 27
is the first element of order(x)
- and if you look at rank(x)
, the 27th element is 1
.
x[order(x)]
# [1] 1 3 4 7 8 9 10 13 14 15 18 19 20 25 27 28 29 30 33 34 39 40 41 42 43 44 46 47 48 49
Difference between sort(), rank(), and order()
sort()
sorts the vector in an ascending order.
rank()
gives the respective rank of the numbers present in the vector, the smallest number receiving the rank 1.
order()
returns the indices of the vector in a sorted order.
for example: if we apply these functions are applied to the vector - c (3, 1, 2, 5, 4)
sort(c (3, 1, 2, 5, 4))
will give c(1,2,3,4,5)
rank(c (3, 1, 2, 5, 4))
will give c(3,1,2,5,4)
order(c (3, 1, 2, 5, 4))
will give c(2,3,1,5,4).
if you put these indices in this order, you will get the sorted vector. Notice how v[2] = 1, v[3] = 2, v[1] = 3, v[5] = 4 and v[4] = 5
also there is a tie handling method in R. If you run rank(c (3, 1, 2, 5, 4, 2))
it will give Rank 1 to 1, since there are two 2 present R will rank them on 2 and 3 but assign Rank 2.5 to each of them, next 3 will get Rank 4.0, so
rank(c (3, 1, 2, 5, 4, 2))
will give you output [4.0 1.0 2.5 6.0 5.0 2.5]
Hope this is helpful.
understanding output of order function in R
I think you are looking for rank
rather than order
but rank
can take only one column value. So we can first order
the data based on names
and then use rank
.
test <- test[order(test$names), ]
rank(-test$scores, ties.method = "first")
#[1] 1 5 2 3 4
See ?rank
for different ties.method
options. If we use ties.method = "first"
when there is a tie small number is given to the entry which occurs first and opposite when ties.method = "last"
.
rank(-test$scores, ties.method = "last")
#[1] 2 5 1 3 4
order
returns indices of the original vector in sorted order.
a1 <- order(test$scores, decreasing = TRUE)
a1
#[1] 1 3 4 5 2
a2 <- test$scores
a2
#[1] 10 5 10 9 8
Here, the output of order
can be interpreted as a2[a1[1]]
(10) is the biggest number followed by a2[a1[2]]
(10) and a2[a1[3]]
(9) and so on.
data
names <- c("Anna", "Bella", "Christian", "Derrick", "Emma")
scores <- c(10,5,10,9,8)
age <- c(16,16,17,18,21)
test <- data.frame(names, scores, age)
How to rank order dates in R
use dplyr::dense_rank()
df %>% mutate(new = dense_rank(date))
acc product date new
1 a1 p1 d1 1
2 a1 p1 d2 2
3 a1 p1 d3 3
4 a1 p1 d4 4
5 a1 p2 d1 1
6 a1 p2 d2 2
7 a1 p2 d3 3
8 a1 p3 d3 3
9 a1 p3 d4 4
If however, you want to restart ranks for each acc
use group_by
before the mutate statement.
dput used
df <- structure(list(acc = c("a1", "a1", "a1", "a1", "a1", "a1", "a1",
"a1", "a1"), product = c("p1", "p1", "p1", "p1", "p2", "p2",
"p2", "p3", "p3"), date = c("d1", "d2", "d3", "d4", "d1", "d2",
"d3", "d3", "d4")), class = "data.frame", row.names = c(NA, -9L
))
Elegant way to obtain order(rank) of each column of a matrix
Perhaps you can try this
> apply(A, 2, rank)
[,1] [,2]
[1,] 2.5 2
[2,] 1.0 3
[3,] 2.5 1
How to get reverse rank order when using chain syntax in R
If we can block the code within {}
, it works as intended
c(5,7,14,3) %>%
{rank(-.)}
#[1] 3 2 1 4
Or make use of multiply_by
c(5,7,14,3) %>%
magrittr::multiply_by(-1) %>%
rank
#[1] 3 2 1 4
ranking dataframe using two columns in R
You can use data.table::frank
or dplyr::min_rank
:
data.table::frank
dt$Rank <- frank(dt, B, A, ties.method = "min")
dt
A B Rank
1 1 1 1
2 2 1 2
3 2 1 2
4 4 4 5
5 5 3 4
dplyr::min_rank
mutate(dt, Rank = min_rank(paste(B,A)))
A B Rank
1 1 1 1
2 2 1 2
3 2 1 2
4 4 4 5
5 5 3 4
Data
dt <- data.frame(A = c(1,2,2,4,5), B = c(1,1,1,4,3))
Related Topics
How to Match Fuzzy Match Strings from Two Datasets
Plotting Contours on an Irregular Grid
Explain a Lazy Evaluation Quirk
Calculating Cumulative Sum For Each Row
How to Export Multiple Data.Frame to Multiple Excel Worksheets
Simpler Population Pyramid in Ggplot2
Split Violin Plot With Ggplot2
How R Formats Posixct With Fractional Seconds
Subset Dataframe by Multiple Logical Conditions of Rows to Remove
How to Put Labels Over Geom_Bar in R With Ggplot2
Special Variables in Ggplot (..Count.., ..Density.., etc.)
Dplyr: How to Use Group_By Inside a Function
Converting Decimal to Binary in R
Remove Columns With Zero Values from a Dataframe
How to Display the Frequency At the Top of Each Factor in a Barplot in R
Alternate, Interweave or Interlace Two Vectors
Cumulatively Paste (Concatenate) Values Grouped by Another Variable
Read All Files in Directory and Apply Multiple Functions to Each Data Frame