基于R的另一列中的数据框中的逐步列总和
框
我 | 数据 |
---|---|
这样 | 的 |
有 | |
| |
| |
| |
| |
| | |
---|---|---|
| | |
| | |
| | |
| 3 | 2.5 |
b | 1 | 5 |
b | 6 | 3 |
平均(x)是X的所有以前实例的平均值,而团队相同。我有以下R码获得总体平均值,但是我正在寻找“逐步”平均值。
new_df <- df %>% group_by(Team) %>% summarise(avg_x = mean(x))
有没有办法对此进行矢量化,同时仅评估每个“迭代”上的先前行?
I have a data frame like this:
Team | GF |
---|---|
A | 3 |
B | 5 |
A | 2 |
A | 3 |
B | 1 |
B | 6 |
Looking for output like this (just an additional column):
Team | x | avg(X) |
---|---|---|
A | 3 | 0 |
B | 5 | 0 |
A | 2 | 3 |
A | 3 | 2.5 |
B | 1 | 5 |
B | 6 | 3 |
avg(x) is the average of all previous instances of x where Team is the same. I have the following R code which gets the overall average, however I'm looking for the "step-wise" average.
new_df <- df %>% group_by(Team) %>% summarise(avg_x = mean(x))
Is there a way to vectorize this while only evaluating the previous rows on each "iteration"?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要
Cummean()
函数从dplyr
,与lag()
:生成以下内容:
根据需要。
编辑1:
AS @ritchie sacramento 指出,以下内容更加清晰,更清晰:
You want the
cummean()
function fromdplyr
, combined withlag()
:Producing the following:
As required.
Edit 1:
As @Ritchie Sacramento pointed out, the following is cleaner and clearer: