计算分组百分比

时间:2018-12-04 14:11:31

标签: r dplyr

对于示例数据框:

df <- structure(list(name = c("a", "b", "c", "d", "e", "f", "g", "h", 
    "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", 
    "v", "w", "x", "y", "z", "a", "b", "c", "d", "e", "f", "g", "h", 
    "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", 
    "v", "w", "x", "y", "z", "a", "b", "c", "d", "e", "f", "g", "h", 
    "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", 
    "v", "w", "x", "y", "z"), amount = c(11L, 9L, 5L, 13L, 15L, 16L, 
    2L, 5L, 6L, 8L, 9L, 15L, 16L, 17L, 13L, 11L, 10L, 9L, 8L, 7L, 
    6L, 8L, 15L, 16L, 15L, 9L, 8L, 7L, 6L, 5L, 18L, 16L, 1L, 14L, 
    15L, 13L, 12L, 11L, 10L, 9L, 8L, 5L, 6L, 9L, 10L, 12L, 13L, 6L, 
    8L, 15L, 16L, 15L, 9L, 8L, 7L, 6L, 5L, 18L, 16L, 1L, 14L, 15L, 
    13L, 12L, 11L, 10L, 9L, 13L, 15L, 16L, 17L, 18L, 19L, 20L, 22L, 
    17L, 16L, 8L), decile = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 
    10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 
    5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 
    4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 
    3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 
    8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L), time = c(2016L, 2016L, 
    2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 
    2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 
    2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 
    2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 
    2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 
    2017L, 2017L, 2017L, 2017L, 2017L, 2018L, 2018L, 2018L, 2018L, 
    2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 
    2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 
    2018L, 2018L, 2018L, 2018L)), .Names = c("name", "amount", "decile", 
    "time"), row.names = c(NA, -78L), class = c("tbl_df", "tbl", 
    "data.frame"), spec = structure(list(cols = structure(list(name = structure(list(), class = c("collector_character", 
    "collector")), amount = structure(list(), class = c("collector_integer", 
    "collector")), decile = structure(list(), class = c("collector_integer", 
    "collector")), time = structure(list(), class = c("collector_integer", 
    "collector"))), .Names = c("name", "amount", "decile", "time"
    )), default = structure(list(), class = c("collector_guess", 
    "collector"))), .Names = c("cols", "default"), class = "col_spec"))

我想产生一个附加的数据框,该数据框详细说明每个“十分位数”中的行数。我还想计算“数量”等于或大于10的比例(占每个十分位数的总行数的百分比)。

我想要一个dplyr解决方案,因为该解决方案正努力使其他程序包能够处理我的真实数据。

1 个答案:

答案 0 :(得分:0)

使用dplyr,我们可以group_by decile并按n()计算每个组中的行数,并计算amount为将这些行的总和除以总行数即可得出大于10的数字。

library(dplyr)

df %>%
  group_by(decile) %>%
  summarise(count_rows = n(), 
           prop = sum(amount > 10)/count_rows)


#  decile count_rows  prop
#    <int>      <int> <dbl>
# 1      1          9 0.556
# 2      2          9 0.444
# 3      3          9 0.444
# 4      4          9 0.556
# 5      5          9 0.667
# 6      6          9 0.667
# 7      7          6 0.333
# 8      8          6 0.333
# 9      9          6 0.500
#10     10          6 0.667