Skip to content

Instantly share code, notes, and snippets.

@justmarkham
Created March 17, 2014 03:24
Show Gist options
  • Save justmarkham/9593453 to your computer and use it in GitHub Desktop.
Save justmarkham/9593453 to your computer and use it in GitHub Desktop.
Smaller example of problem with min_rank
library(dplyr) # using 0.1.3
# generate some test data
set.seed(101)
test <- tbl_df(data.frame(ID=sample(1:5, 50, replace=TRUE), X=sample(101:105, 50, replace=TRUE)))
test <- arrange(test, ID)
people <- group_by(test, ID)
# let's take a look at the data
print(people)
# ID X
# 1 1 103
# 2 1 105
# 3 1 101
# 4 1 104
# 5 1 105
# 6 1 101
# 7 1 102
# 8 1 101
# 9 2 101
# 10 2 103
# 11 2 104
# 12 2 103
# 13 2 102
# 14 2 103
# 15 2 102
# 16 2 104
# 17 2 102
# 18 2 101
# 19 2 102
# 20 2 105
# 21 3 104
# 22 3 103
# 23 3 105
# 24 3 104
# 25 3 105
# 26 3 104
# 27 3 104
# 28 3 101
# 29 3 104
# 30 3 105
# 31 4 104
# 32 4 104
# 33 4 104
# 34 4 105
# 35 4 104
# 36 4 102
# 37 4 105
# 38 4 102
# 39 4 105
# 40 4 104
# 41 4 101
# 42 5 105
# 43 5 105
# 44 5 101
# 45 5 105
# 46 5 105
# 47 5 105
# 48 5 103
# 49 5 101
# 50 5 102
# use row_number: results look correct
mutate(people, new=row_number(desc(X))) %.% arrange(ID, new)
# ID X new
# 1 1 105 1
# 2 1 105 2
# 3 1 104 3
# 4 1 103 4
# 5 1 102 5
# 6 1 101 6
# 7 1 101 7
# 8 1 101 8
# 9 2 105 1
# 10 2 104 2
# 11 2 104 3
# 12 2 103 4
# 13 2 103 5
# 14 2 103 6
# 15 2 102 7
# 16 2 102 8
# 17 2 102 9
# 18 2 102 10
# 19 2 101 11
# 20 2 101 12
# 21 3 105 1
# 22 3 105 2
# 23 3 105 3
# 24 3 104 4
# 25 3 104 5
# 26 3 104 6
# 27 3 104 7
# 28 3 104 8
# 29 3 103 9
# 30 3 101 10
# 31 4 105 1
# 32 4 105 2
# 33 4 105 3
# 34 4 104 4
# 35 4 104 5
# 36 4 104 6
# 37 4 104 7
# 38 4 104 8
# 39 4 102 9
# 40 4 102 10
# 41 4 101 11
# 42 5 105 1
# 43 5 105 2
# 44 5 105 3
# 45 5 105 4
# 46 5 105 5
# 47 5 103 6
# 48 5 102 7
# 49 5 101 8
# 50 5 101 9
# use min_rank: works properly for ID=1, but not for other IDs
mutate(people, new=min_rank(desc(X))) %.% arrange(ID, new)
# ID X new
# 1 1 105 1
# 2 1 105 1
# 3 1 104 3
# 4 1 103 4
# 5 1 102 5
# 6 1 101 6
# 7 1 101 6
# 8 1 101 6
# 9 2 103 1
# 10 2 102 1
# 11 2 103 3
# 12 2 102 3
# 13 2 101 5
# 14 2 101 5
# 15 2 105 5
# 16 2 102 8
# 17 2 104 9
# 18 2 103 9
# 19 2 104 9
# 20 2 102 9
# 21 3 103 1
# 22 3 105 1
# 23 3 104 3
# 24 3 104 4
# 25 3 105 4
# 26 3 104 6
# 27 3 105 7
# 28 3 104 7
# 29 3 101 7
# 30 3 104 7
# 31 4 104 1
# 32 4 104 1
# 33 4 105 3
# 34 4 101 3
# 35 4 104 5
# 36 4 104 5
# 37 4 105 7
# 38 4 104 8
# 39 4 102 8
# 40 4 102 8
# 41 4 105 8
# 42 5 105 1
# 43 5 105 1
# 44 5 105 3
# 45 5 105 4
# 46 5 103 5
# 47 5 101 6
# 48 5 105 6
# 49 5 101 6
# 50 5 102 6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment