每组均值,组中变量数 [英] Mean per group and with count of variables in group

查看:45
本文介绍了每组均值,组中变量数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想生成一个表格,其中包含每个范围的组,均值和每个组中变量的数量.

I would like to generate a table with groups per range, the mean and the count of variables in each group.

我有一个如下所示的data.frame:

I have a data.frame like below:

Variable Shap
    1    0.10
    6    0.50
    7    0.30
    5    0.40
    9    0.10
    9    0.25
    2    0.24
    9    0.23
    5    0.22
    5    0.21
    1    0.20
    4    0.19
    5    0.18
    8    0.17
    6    0.16

并希望获得这样的数据框

And would like to get a dataframe like this

Range  Shap_Avg   Counts
0-5    0.2175000  8
6-9    0.2442857  7

对于分组,意味着我有这段代码,但是我不知道如何包括计数功能

For grouping and mean I have this code, but I don´t know how I can include the count function

# Group and mean
Group <- data %>%
  group_by(Range = cut(Variable, breaks = c(0, 5, 9), 
                          labels = c("0-5", "6-9"))) %>%
  summarise(Shap_Avg = mean(Shap))

推荐答案

使用 dplyr :

df $ Labels <- cut(df$Variable, breaks = c(0,5, 9))

     df %>% 
      group_by(Labels) %>% 
       summarise(Mean = mean(Shap), N = n())
   # A tibble: 2 x 3
  Labels  Mean     N
  <fct>  <dbl> <int>
1 (0,5]  0.218     8
2 (5,9]  0.244     7

数据:

df <- structure(list(Variable = c(1L, 6L, 7L, 5L, 9L, 9L, 2L, 9L, 5L, 
5L, 1L, 4L, 5L, 8L, 6L), Shap = c(0.1, 0.5, 0.3, 0.4, 0.1, 0.25, 
0.24, 0.23, 0.22, 0.21, 0.2, 0.19, 0.18, 0.17, 0.16)), class = "data.frame", row.names = c(NA, 
-15L))

这篇关于每组均值,组中变量数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆