Skip to content

Instantly share code, notes, and snippets.

@juanchiem
Created August 8, 2022 20:32
Show Gist options
  • Save juanchiem/0ad3f029b54cb4d2cd4d8bfaf28d9f6d to your computer and use it in GitHub Desktop.
Save juanchiem/0ad3f029b54cb4d2cd4d8bfaf28d9f6d to your computer and use it in GitHub Desktop.
dat <- tibble(
x_1 = runif(10),
x_2 = runif(10),
x_3 = runif(10)
)
dat |>
mutate(
across(
.cols = everything(),
.fns = rank,
.names = "{.col}_rank"
)
) |>
mutate(
across(
.cols = -matches("rank"),
.fns = cumsum,
.names = "{.col}_cumsum"
)
)
@juanchiem
Copy link
Author

# A tibble: 10 × 9
     x_1   x_2    x_3 x_1_rank x_2_rank x_3_rank x_1_cumsum x_2_cumsum x_3_cumsum
   <dbl> <dbl>  <dbl>    <dbl>    <dbl>    <dbl>      <dbl>      <dbl>      <dbl>
 1 0.778 0.294 0.945         7        3       10      0.778      0.294      0.945
 2 0.780 0.796 0.131         8        8        3      1.56       1.09       1.08 
 3 0.196 0.228 0.233         2        1        4      1.75       1.32       1.31 
 4 0.135 0.350 0.0205        1        4        1      1.89       1.67       1.33 
 5 0.933 0.795 0.123        10        7        2      2.82       2.46       1.45 
 6 0.932 0.359 0.350         9        5        6      3.75       2.82       1.80 
 7 0.727 0.960 0.518         6       10        7      4.48       3.78       2.32 
 8 0.263 0.929 0.845         4        9        9      4.74       4.71       3.16 
 9 0.243 0.377 0.309         3        6        5      4.99       5.09       3.47 
10 0.453 0.281 0.696         5        2        8      5.44       5.37       4.17 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment