R dplyr:基于组的条件突变 [英] R dplyr: Conditional Mutate based on Groups
本文介绍了R dplyr:基于组的条件突变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当前,我正在解决以下问题:
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.
这是使用mpg数据集复制代码的副本.
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()
}
有没有更好的方法不需要for循环?
Is there any better way that does not make a for loop necessary?
谢谢,祝一切顺利!
推荐答案
您可以使用 purrr
中的 map_dbl
来转换for-loop:
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
这篇关于R dplyr:基于组的条件突变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文