簡體   English   中英

在R中找到帶有循環的組的平均值

[英]Finding mean of groups with loop in R

我想找到歸為answer_options的組的平均值。 不幸的是,我什至無法建立結構來解決問題。

 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. 1個
  2. c3 [1。]
  3. c3 [1. + 2。]
  4. c3 [1. + 2. + 3。]。

我對此問題感到不安,希望您能幫助我! 非常感謝。

編輯:為了使我的問題清楚,我編輯一些其他信息。

我不確定是否可以使用數據框而不是矩陣。 我用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

編輯以在下面處理操作注釋

您將需要另一個變量來唯一標識要匯總的每種情況。 如“疑問”。

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

根據您的問題,您想計算組的均值,對嗎? 如果是這樣,下面的代碼將首先計算出每個組的均值(請注意,我將您的輸入轉換為數據框而不是矩陣):

# 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

如果這不是您要的內容,您能否澄清我可能誤解的任何內容? 希望這可以幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM