I am trying to sort a grouped tbl_df data table.
I group by productId
data %>%
group_by(productId) %>%
summarize(Totals = sum(amount, na.rm = TRUE),
count = n())
That's my summary, this looks all good.
# A tibble: 84 x 3
productId Totals count
<dbl> <dbl> <int>
1 16 50000 1
2 26 104770730. 464
3 80 2315000 11
4 100 138487667 444
5 105 41225000 83
6 106 5365000 21
7 126 9762950 43
8 127 7950000 36
9 213 900000 5
10 218 200000 2
Now, I would like to sort by Totals - the summary I created, descending. Or sort by count.
How do I do that? I tried mutate
and I tried,
data %>%
group_by(productId) %>%
summarize(Totals = sum(amount, na.rm = TRUE),
sort(Totals, decreasing = TRUE),
count = n())
This only gives me a duplicated Totals
, column.
# A tibble: 84 x 4
productId Totals `sort(Totals, decreasing = TRUE)` count
<dbl> <dbl> <dbl> <int>
1 16 50000 50000 1
2 26 104770730. 104770730. 464
3 80 2315000 2315000 11
4 100 138487667 138487667 444
5 105 41225000 41225000 83
6 106 5365000 5365000 21
7 126 9762950 9762950 43
8 127 7950000 7950000 36
9 213 900000 900000 5
10 218 200000 200000 2
# … with 74 more rows
Any thoughts? Thank you very much.