'R DPLYR GROUPINGS

library(dplyr)

data(mtcars)

mtcars$FACTORA = sample(c("A", "b"), r=T)

mtcars$FACTORB=sample("c","e")

DATA = mtcars %>%
  group_by(FACTORA, FACTORB) %>%
  slice(which.min(wt)) &
  group_by(FACTORA) %>%
  slice(which.min(wt))

I wish to keep rows that MINIMIZE wt by qsec and gear and also keep rows that minimize wt just by qsec all in one data.

or do i have to do this

DATA = mtcars %>%
  group_by(FACTORA,FACTORB) %>%
  slice(which.min(wt))

DATADATA = mtcars %>%
  group_by(FACTORA) %>%
  slice(which.min(wt))

and then do merge?

enter image description here



Solution 1:[1]

I think this is what you mean (replacing qsec for cyl which is categorical). Note that in this set of groupings the keep2 is a bit extraneous since any row that minimizes wt for each cyl is guaranteed to appear in the rows that minimize wt for each cyl/gear group.

Also, this will only return one minimum and drop ties, though since you use which.min above I figure that isn't important.

library(dplyr)
mtcars %>%
  group_by(cyl, gear) %>%
  arrange(wt) %>%
  mutate(keep1 = row_number() == 1L) %>% 
  group_by(cyl) %>%
  arrange(wt) %>% 
  mutate(keep2 = row_number() == 1L) %>%
  filter(keep1 | keep2)
#> # A tibble: 8 × 13
#> # Groups:   cyl [3]
#>     mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb keep1 keep2
#>   <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <lgl> <lgl>
#> 1  30.4     4  95.1   113  3.77  1.51  16.9     1     1     5     2 TRUE  TRUE 
#> 2  30.4     4  75.7    52  4.93  1.62  18.5     1     1     4     2 TRUE  FALSE
#> 3  21.5     4 120.     97  3.7   2.46  20.0     1     0     3     1 TRUE  FALSE
#> 4  21       6 160     110  3.9   2.62  16.5     0     1     4     4 TRUE  TRUE 
#> 5  19.7     6 145     175  3.62  2.77  15.5     0     1     5     6 TRUE  FALSE
#> 6  15.8     8 351     264  4.22  3.17  14.5     0     1     5     4 TRUE  TRUE 
#> 7  21.4     6 258     110  3.08  3.22  19.4     1     0     3     1 TRUE  FALSE
#> 8  15.2     8 304     150  3.15  3.44  17.3     0     0     3     2 TRUE  FALSE

Created on 2022-04-29 by the reprex package (v2.0.1)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Calum You