Sample of df:
df <- tibble(name = LETTERS[1:10],
x = rnorm(10, mean = 10),
y = rnorm(10, 10),
z = rnorm(10, 10))
I would like to mutate ranked columns for x
, then the sums of cols x
and y
, then x
and y
and z
, where the bigger numbers are ranked 1, then the smallest numbers 10.
Starting with x
, I could do something like:
df %<>% mutate(rank_01 = min_rank(-x))
Which computes the ranked column for x
, but then I'm not sure what the best process would be to compute the latter columns. I'm guessing taking advantage of vectorisation somehow, but my programming skills are limited here.
In my real df, the total number of cols I would like to do this with is >50, so an automated process is ideal!
Expected output:
# A tibble: 10 x 7
name x rank_01 y rank_02 z rank_03
* <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 A 9.37 8 11.5 4 10.9 2
2 B 10.2 6 10.4 5 10.8 3
3 C 9.16 10 9.38 10 10.1 9
4 D 11.6 1 7.79 8 8.01 10
5 E 10.3 5 11.1 2 10.6 1
6 F 9.18 9 9.96 9 9.94 8
7 G 10.5 4 9.98 6 9.84 6
8 H 10.7 2 10.9 1 8.53 7
9 I 10.6 3 10.8 3 9.52 4
10 J 9.69 7 10.6 7 10.4 5
Another approach with
tidyverse
You may also want to set the column names to something like
rank_x
,_rank_xy
, etc. See Cumulatively paste (concatenate) values grouped by another variable for that. E.g.,A different approach using
tidyverse
andreshape2
:Or if you want column names that indicate cumulation: