Sorting by more than one group based on sum of another variable

Hello,

I have a data frame that looks similar to the one below. I need it to be sorted by type, species, sort, and then grade. It is mostly sorted how I want, however I don't want the species groups to be sorted in alphabetical order. I would like them to be sorted descending by the sum of 'Net BdFt' within each species and type group. Could anyone help me use dplyr to achieve this?

Type, Species, Sort, Grade, Gross BdFt, Net BdFt

1 4404 BB 0 9 164 16
2 4404 BB H Y 55 55
3 4404 BM 0 9 20 20
4 4404 BM H 4 631 419
5 4404 BM H Y 242 242
6 4404 BM P P 919 831
7 4404 CH H 4 266 206
8 4404 CH H Y 88 88
9 4404 CH P P 607 565
10 4404 CO H 3 648 531
11 4404 CO H 4 2043 1817
12 4404 CO H Y 31 31
13 4404 CO P P 416 286
14 4404 DF H 2 468 468
15 4404 DF H 3 167 167
16 4404 DF H 4 20214 19740
17 4404 DF H Y 613 613
18 4404 DF P P 5928 5930
39 4410 CH * * 161 161
41 4410 DF * * 18771 18756
46 4410 GF * * 3642 3642
49 4410 RA * * 549 549

Is this what you are looking for?

DF <- read.csv("c:/users/fxcampos/Documents/R/Play/Dummy.csv", sep = " ")
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
SUM <- DF %>% group_by(Type, Species) %>% 
  summarize(Total = sum(Net_BdFt))
SUM
#> # A tibble: 9 x 3
#> # Groups:   Type [2]
#>    Type Species Total
#>   <int> <fct>   <int>
#> 1  4404 BB         71
#> 2  4404 BM       1512
#> 3  4404 CH        859
#> 4  4404 CO       2665
#> 5  4404 DF      26918
#> 6  4410 CH        161
#> 7  4410 DF      18756
#> 8  4410 GF       3642
#> 9  4410 RA        549
DF <- inner_join(DF, SUM, by = c("Type", "Species")) %>% 
  arrange(Type, desc(Total), Sort, Grade) %>% 
  select(-Total)
DF
#>    Type Species Sort Grade Gross_BdFt Net_BdFt
#> 1  4404      DF    H     2        468      468
#> 2  4404      DF    H     3        167      167
#> 3  4404      DF    H     4      20214    19740
#> 4  4404      DF    H     Y        613      613
#> 5  4404      DF    P     P       5928     5930
#> 6  4404      CO    H     3        648      531
#> 7  4404      CO    H     4       2043     1817
#> 8  4404      CO    H     Y         31       31
#> 9  4404      CO    P     P        416      286
#> 10 4404      BM    0     9         20       20
#> 11 4404      BM    H     4        631      419
#> 12 4404      BM    H     Y        242      242
#> 13 4404      BM    P     P        919      831
#> 14 4404      CH    H     4        266      206
#> 15 4404      CH    H     Y         88       88
#> 16 4404      CH    P     P        607      565
#> 17 4404      BB    0     9        164       16
#> 18 4404      BB    H     Y         55       55
#> 19 4410      DF    *     *      18771    18756
#> 20 4410      GF    *     *       3642     3642
#> 21 4410      RA    *     *        549      549
#> 22 4410      CH    *     *        161      161

Created on 2020-04-30 by the reprex package (v0.3.0)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.