【问题标题】:Finding mean of groups with loop in R在R中找到带有循环的组的平均值
【发布时间】:2016-08-23 13:17:20
【问题描述】:

我想找到归类为 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.] . . .

我被这个问题困扰着,希望你能帮助我!非常感谢。

编辑:为了清楚地提出我的问题,我编辑了一些附加信息。

【问题讨论】:

  • 不清楚你需要做什么
  • 试试ave(options, answer_options, FUN=mean),它计算options的平均值,按answer_options分组。
  • 非常感谢 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

标签: r loops for-loop matrix


【解决方案1】:

我不确定数据框是否适合您而不是矩阵。我使用 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

在此处编辑以解决 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

【讨论】:

  • 在重新阅读您的帖子时,我不确定这是否是您所要求的。我不太明白你要求的输出。
  • 主要是正确的。但它在一种情况下无法正常工作。如果我有一个新组(第 4 组)再次有 4 个答案选项,则代码将取 3. 和 4. 的平均值。请尝试使用这些 answer_options
【解决方案2】:

根据您的问题,您要计算组均值的均值,对吗?如果是这样,下面的代码将首先计算出每个组的平均值(请注意,我将您的输入转换为数据框而不是矩阵):

# 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

如果这不是您所要求的,您能否澄清我可能误解的任何内容?希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 2017-03-22
    • 1970-01-01
    • 2021-08-31
    • 2021-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-29
    相关资源
    最近更新 更多