smz smz - 5 months ago 18
R Question

How to rank rows by two columns at once in R?

Here is the code to rank based on column v2:

x <- data.frame(v1 = c(2,1,1,2), v2 = c(1,1,3,2))
x$rank1 <- rank(x$v2, ties.method='first')


But I really want to rank based on both v2 and/then v1 since there are ties in v2. How can I do that without using RPostgreSQL?

Answer Source

How about:

within(x, rank2 <- rank(order(v2, v1), ties.method='first'))

#   v1 v2 rank1 rank2
# 1  2  1     1     2
# 2  1  1     2     1
# 3  1  3     4     4
# 4  2  2     3     3