Reputation: 2859
I have a code as below. First the code should run based on the dplyr::arrange(desc(sum.total))
then dplyr::arrange(desc(sum.mpg))
then as the outcome I would like to have a dataframe/datatable. Many thanks in advance
head(mtcars)
my.mtcars <- mtcars %>%
dplyr::group_by(gear)%>%
dplyr::summarise(
sum.total = n(),
sum.mpg = sum(mpg))%>%
dplyr::arrange(desc(sum.total)) # Run me first
#dplyr::arrange(desc(sum.mpg)) # Second Dont run the line above but this
Expected Answer
#with dplyr::arrange(desc(sum.total))
gear sum.total sum.mpg
1 3 15 242.
2 4 12 294.
3 5 5 107.
#with dplyr::arrange(desc(sum.mpg))
gear sum.total sum.mpg
1 4 12 294.
2 3 15 242.
3 5 5 107.
Upvotes: 0
Views: 55
Reputation: 8572
my.mtcars <- mtcars %>%
dplyr::group_by(gear)%>%
dplyr::summarise(
sum.total = n(),
sum.mpg = sum(mpg)
lapply(c('sum.total', 'sum.mpg'), function(var) arrange(my.mtcars, desc(.data[[var]])))
Upvotes: 2