2016-08-23 69 views
0

我想找到歸類爲answer_options的組的平均值。不幸的是,我甚至無法建立結構來解決問題。查找帶有循環的組的平均值R

answer_options <- c(3,3,3,2,2,4,4,4,4) 
options <- c(33,32,31,10,15,5,5,6,6) 
dd <- data.matrix(cbind(answer_options,options)) 

爲了計算然後找到組的平均值,我需要找到第一組有3個值是32,32,31。然後通過1.group計算第一個均值,然後開始answer_options [1 + 3] - 這是2-然後再次重複。

例如:

1.group:C(3,3,3-)和其平均值等於平均(33,32,31) 2.group:C(2,2)和其平均值平等意思是(10,15) 3.group:c(4,4,4,4)及其平均值等於平均值​​(5,5,6,6)

然後我需要計算均值的均值。

c3 <- answer_options 
##for i do not know how? 
a1 <- c3[1]+1 
a2 <- c3[a1] 
a3 <- c3[a1+c3[a1]] 
a4 <- c3[c3[a1+c3[a1]]] 
a5 <- c3[c3[1]+1 +c3[a1]+c3[a1+c3[a1]]] 

序列應該是這樣的:

  1. C3 [1。]
  2. C3 [1 + 2。]
  3. C3 [1 + 2。 +3。] 。 。 。

我很擔心這個問題,我希望你能幫助我!非常感謝。

編輯:爲了清楚地問我的問題,我編輯了一些額外的信息。

+3

不清楚你需要做什麼 – Sotos

+0

嘗試'ave(options,answer_options,FUN = mean)',它計算'options'的平均值,由'answer_options'分組。 –

+0

非常感謝ave(options,answer_options,FUN = mean)選項。然而,作爲下一步,我需要計算均值的均值。而這個選項提供了這個。我不知道如何計算手段事件雖然獨特的功能看起來像工作,但我有一個大數據工作。 [1] 32.0 32.0 32.0 12.5 12.5 5.5 5.5 5.5 5.5 –

回答

0

我不確定數據框是否適用於您而不是矩陣。我使用dplyr來做你正在問的問題。我不是專家程序員,所以這可能是低效的。

answer_options <- c(3,3,3,2,2,4,4,4,4) 
options <- c(33,32,31,10,15,5,5,6,6) 
dd <- data.frame(cbind(answer_options,options)) 

使用dplyr了%>%的管道功能,爲您提供了一個數據幀的摘要信息:

library(dplyr) 
    new.dd <- dd %>% group_by(answer_options) %>% 
    summarise(n=n(), 
       mean_answer_options=mean(options)) 


    answer_options  n mean_answer_options 
      (dbl) (int)    (dbl) 
1    2  2    12.5 
2    3  3    32.0 
3    4  4     5.5 

然後合併這兩個表。

merged.dd<-left_join(dd,new.dd,by="answer_options") 
merged.dd 
    answer_options options n mean_answer_options 
1    3  33 3    32.0 
2    3  32 3    32.0 
3    3  31 3    32.0 
4    2  10 2    12.5 
5    2  15 2    12.5 
6    4  5 4     5.5 
7    4  5 4     5.5 
8    4  6 4     5.5 
9    4  6 4     5.5 

編輯以ADDRESS OP的評論如下這裏

你會需要另一個唯一標識要彙總每種情況的變量。如「問題」。

question<-c(1,1,1,2,2,3,3,3,3,4,4,4,4) 
answer_options <- c(3,3,3,2,2,4,4,4,4,4,4,4,4) 
options <- c(33,32,31,10,15,5,5,6,6,1,1,2,2) 

dd <- data.frame(cbind(question,answer_options,options)) 
dd 

library(dplyr) 
new.dd <- dd %>% group_by(question) %>% 
    summarise(n=n(),mean_options_question=mean(options)) 
new.dd 

merged.dd<-left_join(dd,new.dd,by="question") 
merged.dd 

這會給你下面的輸出。

question answer_options options n mean_options_question 
1   1    3  33 3     32.0 
2   1    3  32 3     32.0 
3   1    3  31 3     32.0 
4   2    2  10 2     12.5 
5   2    2  15 2     12.5 
6   3    4  5 4     5.5 
7   3    4  5 4     5.5 
8   3    4  6 4     5.5 
9   3    4  6 4     5.5 
10  4    4  1 4     1.5 
11  4    4  1 4     1.5 
12  4    4  2 4     1.5 
13  4    4  2 4     1.5 
+0

在重新閱讀您的文章時,我不確定這是您要求的內容。我不太瞭解你的要求輸出。 – akaDrHouse

+0

主要是正確的。但它不能很好地工作,因爲我想在一種情況下。如果我有一個新的組(第4組),其中有4個答案選項,代碼將一起取平均3.和4.。請嘗試使用這些 answer_options < - c(3,3,3,2,2,4,4,4,4,4,4,4,4) options <-c(33,32,31, 10,15,5,5,6,6,1,1,2,2) dd < - data.frame(cbind(answer_options,options)) –

0

根據你的問題,你想計算組的手段,我是否正確?如果是的話,下面的代碼將先計算出各組的方式(請注意,我把你輸入到數據幀,而不是一個矩陣):

# Your input as a dataframe and not a matrix 
> answer_options <- c(3,3,3,2,2,4,4,4,4) 
> options <- c(33,32,31,10,15,5,5,6,6) 
> dd <- data.frame(cbind(answer_options,options)) 

# Calculates the mean of each group and puts it into a "mean_ 
# _answer_options" vector 
> mean_answer_options = by(dd$options,answer_options, FUN = mean) 
> mean_answer_options 
answer_options: 2 
[1] 12.5 
------------------------------------------------------------------------------------------- 
answer_options: 3 
[1] 32 
-------------------------------------------------------------------------------------------- 
answer_options: 4 
[1] 5.5 

就可以計算出各組的平均值的平均通過使用以下命令:

> mean(as.numeric(mean_answer_options)) 
[1] 16.66667 

這會爲每組的方法生成正確的平均值16.66667。這可以通過鉤稽:

> (12.5+32+5.5)/3 
[1] 16.66667 

如果這不是你問什麼,你能澄清什麼,我可能有誤解?希望這可以幫助!