2016-01-28 85 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

相關問題