【问题标题】:Calculate the mean of values assigned to each quantile in different quantile types?计算分配给不同分位数类型中每个分位数的值的平均值?
【发布时间】:2016-01-28 11:33:20
【问题描述】:

我想比较 9 种分位数。

我计算了 data.frame 中变量 a 的分位数。对于每种类型 (1-9),我计算了 10 个分位数(其中 1 为最高的 10%,10 为最低的 10%)。

set.seed(123)
library(dplyr)
a <- as.numeric(sample(1.1e6:87e6, 366, replace=T))
b <- runif(366, 0.005, 2.3)
df<- data.frame(a,b)
df <- df %>% 
      mutate(type1 = 11 - as.integer(cut(a, quantile(a, probs=0:10/10, type = 1), include.lowest=TRUE)),  
             type2 = 11 - as.integer(cut(a, quantile(a, probs=0:10/10, type = 2), include.lowest=TRUE)),
             type3 = 11 - as.integer(cut(a, quantile(a, probs=0:10/10, type = 3), include.lowest=TRUE)),
             type4 = 11 - as.integer(cut(a, quantile(a, probs=0:10/10, type = 4), include.lowest=TRUE)),
             type5 = 11 - as.integer(cut(a, quantile(a, probs=0:10/10, type = 5), include.lowest=TRUE)),
             type6 = 11 - as.integer(cut(a, quantile(a, probs=0:10/10, type = 6), include.lowest=TRUE)),
             type7 = 11 - as.integer(cut(a, quantile(a, probs=0:10/10, type = 7), include.lowest=TRUE)),
             type8 = 11 - as.integer(cut(a, quantile(a, probs=0:10/10, type = 8), include.lowest=TRUE)),
             type9 = 11 - as.integer(cut(a, quantile(a, probs=0:10/10, type = 9), include.lowest=TRUE)))

我想计算 9 种类型的第 10 个分位数中的 a 的平均值。我应该有 a 的 90 个平均值。
我该怎么做?

【问题讨论】:

  • 你的分位数都是一样的。我假设您正在寻找类似df %&gt;% group_by(type1) %&gt;% summarise_each(funs(mean))?
  • 你为什么不只看分位数?
  • @42- 因为我的分析涉及获取每个分位数的均值并将其乘以另一个参数。

标签: r dplyr mean


【解决方案1】:

继续使用dplyr,您可以使用lapply 循环遍历分位数列,使用group_by_ 一次一个,使用summarise 计算分组均值。 do.call(cbind ... 捕获方法列并将它们转换为新的data.frame

means_a <- do.call(cbind, lapply(names(df)[3:11], function(x){group_by_(df, x) %>%
    summarise(m = mean(a)) %>% select(m)}))
# clean up names
names(means_a) <- names(df)[3:11]

你还剩下

> means_a
      type1    type2    type3    type4    type5    type6    type7    type8    type9
1  82835646 82835646 82704531 82704531 82704531 82835646 82704531 82835646 82835646
2  73922430 73922430 73809597 73674619 73809597 73922430 73809597 73922430 73922430
3  64571479 64571479 64449537 64328263 64449537 64449537 64449537 64449537 64449537
4  56421583 56421583 56320527 56207920 56320527 56320527 56320527 56320527 56320527
5  47065506 47065506 47065506 46924157 47065506 47065506 47065506 47065506 47065506
6  38559879 38559879 38468169 38468169 38468169 38468169 38559879 38468169 38468169
7  31639898 31639898 31541934 31442833 31541934 31541934 31639898 31541934 31541934
8  23589748 23589748 23495235 23373569 23495235 23495235 23589748 23495235 23495235
9  15766101 15766101 15645916 15535787 15645916 15535787 15766101 15535787 15645916
10  6637675  6637675  6637675  6500634  6637675  6500634  6637675  6500634  6637675

【讨论】:

  • 感谢您使用 dplyr 解决它。我真的很感激。
  • 我还有一个问题。如果我在 data.frame 中有另外两列,一列用于日期,一列用于工作日(周一至周日)。过滤工作日后如何计算 b 的平均值
  • 如果你只想要每个工作日b 的整体平均值,你可以使用df %&gt;% group_by(weekday) %&gt;% summarise(mean(b))。如果你想要像上面的a 这样的分位数,请将上面版本中的dplyr 链更改为df %&gt;% filter(weekday == 'Friday') %&gt;% group_by_(x) %&gt;% summarise(m = mean(b)) %&gt;% select(m)
【解决方案2】:

这是产生所需 90 均值的一种方法:

f <- function(type, x) {return(11 - as.integer(cut(x, quantile(x, probs=0:10/10, type = type), include.lowest=TRUE)))}

set.seed(123)
a <- as.numeric(sample(1.1e6:87e6, 366, replace=T))
b <- runif(366, 0.005, 2.3)
df<- data.frame(a,b)
df <- cbind(df, data.frame(sapply(seq(1:9), f, x = df$a)))
sapply(df[, 3:11], function(x) tapply(df$a, x, mean))
             X1       X2       X3       X4       X5       X6       X7       X8       X9
1  82835646 82835646 82704531 82704531 82704531 82835646 82704531 82835646 82835646
2  73922430 73922430 73809597 73674619 73809597 73922430 73809597 73922430 73922430
3  64571479 64571479 64449537 64328263 64449537 64449537 64449537 64449537 64449537
4  56421583 56421583 56320527 56207920 56320527 56320527 56320527 56320527 56320527
5  47065506 47065506 47065506 46924157 47065506 47065506 47065506 47065506 47065506
6  38559879 38559879 38468169 38468169 38468169 38468169 38559879 38468169 38468169
7  31639898 31639898 31541934 31442833 31541934 31541934 31639898 31541934 31541934
8  23589748 23589748 23495235 23373569 23495235 23495235 23589748 23495235 23495235
9  15766101 15766101 15645916 15535787 15645916 15535787 15766101 15535787 15645916
10  6637675  6637675  6637675  6500634  6637675  6500634  6637675  6500634  6637675

注意:添加缺少的功能。

【讨论】:

  • 感谢您的宝贵时间和帮助。
猜你喜欢
  • 2013-11-05
  • 2021-03-03
  • 2016-08-01
  • 2023-01-20
  • 1970-01-01
  • 1970-01-01
  • 2011-10-23
  • 2015-09-02
  • 1970-01-01
相关资源
最近更新 更多