r - Creating multi-field group rankings -
For the following simplified data frames, I have calculated the rank for each school in each school within each test. The problem is that my rank is constant, and I want to reset it with every change in the school-state-test group.
School & lt; -rep (c ("A", "B", "C", "D"), each = 10) state & lt; -rep (c ("NY", "NJ"), time = 20) Test & lt; -rep (c ("LSAT", "MCAT", "GRE", "TOEFL", "ACT"), time = 8) grade & lt; -trunc (Representative ((seq (= 500, = 600, length.out = 4)))) dat & lt; -data.frame (test, state, school, grade) When I add this ranking argument,
$ rank test state school grade rank 10 act NJ A533 1 30 At NJ C 533 2 20 ATT NJ B360340 Act NJ D600 4 5 Act N.Y. A500 5 25 etn NY C 500 6 15 ET NY B 566 735 Act NY D 566 8 18 GRE NJ B533 9 38 GRE NJ D 533 10 8 GRE NJ A 600 11 28 GRE NJ C 600 12 13 GRE New B 500 13 33 GRE New D 500 14 3 GRE Nue 566 15 23 GRE New C 566 16 6 LSAT NJ A533 17 26 LCAT NJ C5 33 18 16 LSAT NJ B600 1 9 36 LSAT NJ D600 20 I They want to look like this. Please note that Tie Breaker is a school. If School A and School C are on grade 533 on both NJs, then School A will be based on first alpha-order. It is fine that low scores have lower ranks.
Test State School Grade Rank 10 Act NJ A 533 1 30 At NJC 533 2 20 Act NJ B360340 At NJ D600 4 5 Act NY A 500 1 25 et NYC 500 2 15 Act NY B 566 3 35 Act NY D 566 4 18 GRE NJ B 533 1 38 GRE NJ D 533 2 8 GRE NJ A 600 3 28 GRE NJ C 600 4 13 GRE NY B 500 1 33 GRE ND 500 2 3 GRE New York 566 3 23 GRE New C 566 4 6 LSAT NJ A533 1 26 LSAT NJ C533 2 16 LSAT NJ B600 3 36 LSAT NJ D600 4 I have a guide about this I appreciate how this rank Ng bring within problem groups.
dplyr library (dplyr ) Dat% & gt;% group_by (Test, State)% & gt;% mutate (rank = row_number ())% & gt;% (Test, State, Rank)% & gt;% head () # Test State School Grade Rank # 1 Act NJ A533 1 # 2 Act NJ C 533 2 # 3 NJ B 600 3 # 4 Act NJ D600 4 # 5 ACT NY A 500 # 1 # ACT ACT 2 C /
Comments
Post a Comment