使用 dplyr 在排除当前观察的同时计算组平均值

编程入门 行业动态 更新时间:2024-10-24 18:22:55
本文介绍了使用 dplyr 在排除当前观察的同时计算组平均值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

使用 dplyr(最好),我试图计算每个观察的组平均值,同时从组中排除该观察.

Using dplyr (preferably), I am trying to calculate the group mean for each observation while excluding that observation from the group.

看起来这应该可以通过 rowwise() 和 group_by() 的组合来实现,但是这两个函数不能同时使用.

It seems that this should be doable with a combination of rowwise() and group_by(), but both functions cannot be used simultaneously.

给定这个数据框:

df <- data_frame(grouping = rep(LETTERS[1:5], 3), value = 1:15) %>% arrange(grouping) df #> Source: local data frame [15 x 2] #> #> grouping value #> (chr) (int) #> 1 A 1 #> 2 A 6 #> 3 A 11 #> 4 B 2 #> 5 B 7 #> 6 B 12 #> 7 C 3 #> 8 C 8 #> 9 C 13 #> 10 D 4 #> 11 D 9 #> 12 D 14 #> 13 E 5 #> 14 E 10 #> 15 E 15

我想获得每个观察的组均值,该观察被排除在组之外,结果:

I'd like to get the group mean for each observation with that observation excluded from the group, resulting in:

#> grouping value special_mean #> (chr) (int) #> 1 A 1 8.5 # i.e. (6 + 11) / 2 #> 2 A 6 6 # i.e. (1 + 11) / 2 #> 3 A 11 3.5 # i.e. (1 + 6) / 2 #> 4 B 2 9.5 #> 5 B 7 7 #> 6 B 12 4.5 #> 7 C 3 ...

我尝试将 rowwise() 嵌套在由 do() 调用的函数中,但没有让它工作,如下所示:

I've attempted nesting rowwise() inside a function called by do(), but haven't gotten it to work, along these lines:

special_avg <- function(chunk) { chunk %>% rowwise() #%>% # filter or something...? } df %>% group_by(grouping) %>% do(special_avg(.))

推荐答案

无需定义自定义函数,而是我们可以简单地将组的所有元素相加,减去当前值,然后除以每组的元素数减去1.

No need to define a custom function, instead we could simply sum all elements of the group, subtract the current value, and divide by number of elements per group minus 1.

df %>% group_by(grouping) %>% mutate(special_mean = (sum(value) - value)/(n()-1)) # grouping value special_mean # (chr) (int) (dbl) #1 A 1 8.5 #2 A 6 6.0 #3 A 11 3.5 #4 B 2 9.5 #5 B 7 7.0

更多推荐

使用 dplyr 在排除当前观察的同时计算组平均值

本文发布于:2023-10-24 09:36:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1523593.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:平均值   dplyr

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!