创建新的df并在R中按组记录百分比

时间:2018-12-04 12:19:27

标签: r

对于示例数据框:

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

我希望生成一个汇总表,其中详细列出了按十分位数计算的项目数。下面列出了一种解决方案:

data.frame(table(df$decile))

除此之外,我想添加一列来记录“名称”(基本行)的百分比,其“数量”大于或等于10 BY分位数。

有什么想法可以完成我的编码吗?

1 个答案:

答案 0 :(得分:1)

如果您不介意使用tidyverse:

app-cell

按十分位数计算多少行的window.performance.navigation.type值> =10。这就是您想要的吗? (您指的是“行”而不是“名称”的百分比?)