Rondo Bohrens
Rondo Bohrens

Reputation: 27

R dplyr: Conditional Mutate based on Groups

Currently, I am working on the following problem:

I am trying to split my dataset in groups and create a new variable that captures the group mean of all opposite cases that do not belong to this group - for a specific time frame.

Here is a replica of my code using the mpg dataset.

cars <- mpg

cars$other_cty_yearly_mean <- 0

for(i in cars$cyl){
  cars <- cars %>%
    group_by(year) %>%
    mutate(other_cty_yearly_mean = if_else(
      cyl == i,
      mean(cty[cyl != i]),
      other_cty_yearly_mean
    )) %>%
    ungroup() %>%
    as.data.frame()
}

Is there any better way that does not make a for loop necessary?

Thanks and best!

Upvotes: 0

Views: 302

Answers (1)

kath
kath

Reputation: 7724

You can use map_dbl from purrr to transform your for-loop:

mpg %>% 
  group_by(year) %>% 
  mutate(other_cty_yearly_mean = map_dbl(cyl, ~ mean(cty[!cyl %in% .x])))

# A tibble: 234 x 12
# Groups:   year [2]
#   manufacturer model      displ  year   cyl trans      drv     cty   hwy fl    class   other_cty_yearly_mean
#   <chr>        <chr>      <dbl> <int> <int> <chr>      <chr> <int> <int> <chr> <chr>                   <dbl>
# 1 audi         a4           1.8  1999     4 auto(l5)   f        18    29 p     compact                  14.6
# 2 audi         a4           1.8  1999     4 manual(m5) f        21    29 p     compact                  14.6
# 3 audi         a4           2    2008     4 manual(m6) f        20    31 p     compact                  14.7
# 4 audi         a4           2    2008     4 auto(av)   f        21    30 p     compact                  14.7
# 5 audi         a4           2.8  1999     6 auto(l5)   f        16    26 p     compact                  17.6
# ... with 229 more rows

Upvotes: 1

Related Questions