2016-01-28 78 views
0

我想比较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的平均值。我应该有90个值的平均值为a
我该怎么做?

+1

你的位数都是一样的。我假设你正在寻找像'df%>%group_by(type1)%>%summarise_each(funs(mean))''? – alistaire

+0

你为什么不看着分位数? –

+0

@ 42- 因为我的分析涉及获取每个分位数的平均值并将其乘以另一个参数。 – aelwan

回答

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 
+0

感谢您使用dplyr解决此问题。我真的很感激。 – aelwan

+0

我还有一个问题。如果我在data.frame中有另外两列的日期和星期一(星期一到星期日)有两列。如何计算每周工作日过滤后的平均值 – aelwan

+1

如果您只想每个工作日的总体平均值为'b',您可以使用'df%>%group_by(工作日)%>%summarize(平均值(b)) '。如果你想分位数的意思就像上面的'a',把上面版本中的'dplyr'链改成'df%>%filter(weekday =='Friday')%>%group_by_(x)%>%summarize( m =平均值(b))%>%select(m)'。 – alistaire

1

这是一种方法,其产生所需的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 

注意:添加缺少的功能。

+0

感谢您的时间和帮助。 – aelwan