R dplyr: Conditional Mutate based on Groups

落爺英雄遲暮 提交于 2021-01-29 06:21:05

问题


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!


回答1:


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


来源:https://stackoverflow.com/questions/52186644/r-dplyr-conditional-mutate-based-on-groups

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!