1
dat <- data.frame(loc.id = rep(1:2, each = 3), 
              year = rep(1981:1983, times = 2), 
              prod = c(200,300,400,150,450,350),
              yld = c(1200,1250,1200,3000,3200,3200))

If I want to select for each loc.id distinct values of yld, I do this:

dat %>% group_by(loc.id) %>% distinct(yld)  

    loc.id     yld
    <int>     <dbl>
      1      1200
      1      1250
      2      3000
      2      3200

However, what I want to do is for loc.id, if years have the same yld, then select the yld with a lower prod value. My dataframe should look like i.e. I want the prod and year column too included in the final dataframe

    loc.id    year   prod     yld 
      1        1981   200     1200
      1        1982   300     1250
      2        1981   150     3000
      2        1983   350     3200   
89_Simple
  • 3,393
  • 3
  • 39
  • 94

1 Answers1

4

We can do an arrange by 'prod' and then slice the first observation

dat %>% 
    arrange(loc.id, prod) %>% 
    group_by(loc.id, yld) %>%
    slice(1)
# A tibble: 4 x 4
# Groups:   loc.id, yld [4]
#  loc.id  year  prod   yld
#   <int> <int> <dbl> <dbl>
#1      1  1981   200  1200
#2      1  1982   300  1250
#3      2  1981   150  3000
#4      2  1983   350  3200
akrun
  • 874,273
  • 37
  • 540
  • 662