0

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))
Jaap
  • 81,064
  • 34
  • 182
  • 193
Theresa M.
  • 77
  • 1
  • 7

1 Answers1

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))
NelsonGon
  • 13,015
  • 7
  • 27
  • 57