简体   繁体   English

使用分组的摘要按因子对另一个data.frame列进行操作

[英]Use grouped summary to operate in another data.frame column by factor

I want to compute a summary of a grouped data.frame , for example. 例如,我想计算分组的data.framesummary

df_summ = mtcars %>% group_by(am) %>% summarise(mean_mpg=mean(mpg))

     am mean_mpg
  (dbl)    (dbl)
1     0 17.14737
2     1 24.39231

In order to later transform another data.frame that shares the same factor levels, but not the number of rows. 为了以后进行转换,另一个data.frame共享相同的因子级别,但没有行数。 For example, calculating the absolute difference from each group's mean of the single values. 例如,从单个值的每个组的平均值计算绝对差。

Here's the toy example 这是玩具的例子

toy=data.frame(am=c(1,1,0,0),mpg=c(1,2,3,4))

The calculation I would like to do would be y = abs(toy$mpg- df_summ$mean_mpg) by factor. 我想做的计算是y = abs(toy$mpg- df_summ$mean_mpg)

My head tells me dplyr must be able to do this but I can't come up with a way. 我的头告诉我dplyr必须能够做到这一点,但我想不出办法。 I want to keep the original data.frame (as in, using mtcars %>% group_by(am) %>% mutate(...) ) 我想保留原始的data.frame(例如,使用mtcars %>% group_by(am) %>% mutate(...)

The expected output looks like that 预期的输出看起来像这样

toy
  am mpg expected
1  1     1 23.39231
2  1     2 22.39231
3  0     3 14.14737
4  0     4 13.14737

Join the two data frames and then perform the calculation: 连接两个数据框,然后执行计算:

toy %>% 
    left_join(df_summ) %>% 
    mutate(y = abs(mpg - mean_mpg))

giving: 赠送:

Joining, by = "am"
  am mpg mean_mpg        y
1  1   1 24.39231 23.39231
2  1   2 24.39231 22.39231
3  0   3 17.14737 14.14737
4  0   4 17.14737 13.14737

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM