I have a data frame with a grouping variable ‘ID’ and some values (‘Value’):
dt <- data.frame(
ID = c('A1','A2','A4','A2','A1','A4','A3','A2','A1','A3'),
Value = c(4,3,1,3,4,6,6,1,8,4)
)
dt
# ID Value
# 1 A1 4
# 2 A2 3
# 3 A4 1
# 4 A2 3
# 5 A1 4
# 6 A4 6
# 7 A3 6
# 8 A2 1
# 9 A1 8
# 10 A3 4
I can calculate an overall rank order of the ‘Value’ column like this:
dt$Order <- rank(dt$Value, ties.method = "first")
dt
# ID Value Order
# 1 A1 4 5
# 2 A2 3 3
# 3 A4 1 1
# 4 A2 3 4
# 5 A1 4 6
# 6 A4 6 8
# 7 A3 6 9
# 8 A2 1 2
# 9 A1 8 10
# 10 A3 4 7
But how can I calculate rank order within each ‘ID’, instead of a global rank order?
# ID Value rnk
# 1 A1 4 1
# 2 A2 3 2
# 3 A4 1 1
# 4 A2 3 3
# 5 A1 4 2
# 6 A4 6 2
# 7 A3 6 2
# 8 A2 1 1
# 9 A1 8 3
# 10 A3 4 1
In T-SQL, we can get this done as the following syntax:
RANK() OVER ( [ < partition_by_clause > ] < order_by_clause > )
Any idea?
My way but there’s likely better. Never used rank, din’t even know about it. Thanks, may be useful.
Yields:
EDIT:
If you don’t care about preserving the original order of the data then this works with less code: