Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

dataframe

r

rank

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?

like image 747
smz Avatar asked Dec 02 '14 22:12

smz


2 Answers

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
like image 102
Matthew Plourde Avatar answered Nov 08 '22 22:11

Matthew Plourde


order works, but for manipulating data frames, also check out the plyr and dplyr packages.

> arranged_x <- arrange(x, v2, v1)
like image 44
mmuurr Avatar answered Nov 08 '22 20:11

mmuurr