Mean per group and with count of variables in group [duplicate]

≯℡__Kan透↙ 提交于 2021-02-05 10:27:19

问题


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

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))

回答1:


With 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

Data:

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))


来源:https://stackoverflow.com/questions/58281267/mean-per-group-and-with-count-of-variables-in-group

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!