[英]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. 这是我使用mpg数据集的代码的副本。
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? 有没有更好的方法不需要for循环?
Thanks and best! 谢谢,最好的!
You can use map_dbl
from purrr
to transform your for-loop: 您可以使用
map_dbl
的purrr
转换for循环:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.