sedsiv
sedsiv

Reputation: 547

R Tibble: Arrange by two columns

Let's say I have the following data:

set.seed(123)
test <- tibble(
  ID = sample(rep(1:100, rpois(100,4))),
  vals = abs(round(rnorm(length(ID), 10000, 5000)))
)

I would like to sort test first by vals and then by ID with the desired output looking like this:

# A tibble: 409 x 2
     ID  vals
   <int> <dbl>
1     48 26522
2     48 14427
3     48  7570
4     48  5922
5     92 25286
6     92 10436
7     92  5705
8     92  4036
9     92  3399
10    64 22190
# ... with 399 more rows

i.e. it should group the sorting by ID and then in decreasing order vals.

What I tried:

test %>% arrange(ID, desc(vals)) 
test %>% arrange(desc(vals), ID) 
test %>% arrange(ID) %>% arrange(desc(vals)) 

Upvotes: 0

Views: 246

Answers (1)

r2evans
r2evans

Reputation: 160687

I think you're missing some clarity:

sort first by each ID's maximum value, descending;

Try this:

library(dplyr)
test %>%
  mutate(valrank = dense_rank(-vals)) %>%
  group_by(ID) %>%
  mutate(valrank = min(valrank)) %>%
  ungroup() %>%
  arrange(valrank, ID, desc(vals))
# # A tibble: 409 x 3
#       ID  vals valrank
#    <int> <dbl>   <int>
#  1    48 26522       1
#  2    48 14427       1
#  3    48  7570       1
#  4    48  5922       1
#  5    92 25286       2
#  6    92 10436       2
#  7    92  5705       2
#  8    92  4036       2
#  9    92  3399       2
# 10    64 22190       3
# # ... with 399 more rows

(I kept valrank just for demonstration.)

Upvotes: 2

Related Questions