【问题标题】:calculating mean by keeping all variable in the dataset in r通过将数据集中的所有变量保存在 r 中来计算平均值
【发布时间】:2020-01-30 06:06:02
【问题描述】:

我试图通过将最终数据集中的所有变量保存在 dplyr 包中来计算时间平均值。 我的示例数据集如下所示:

library(dplyr)
id <-     c(1,1,1,1, 2,2,2,2, 3,3,3,3, 4,4,4,4)
gender <- c(1,1,1,1, 2,2,2,2, 2,2,2,2, 1,1,1,1)
item.id <-c(1,1,1,2, 1,1,2,2, 1,2,3,4, 1,2,2,3)
sequence<-c(1,2,3,1, 1,2,1,2, 1,1,1,1, 1,1,2,1)
time <-   c(5,6,7,1, 2,3,4,9, 1,2,3,9, 5,6,7,8)
data <- data.frame(id, gender, item.id, sequence, time)
> data
   id gender item.id sequence time
1   1      1       1        1    5
2   1      1       1        2    6
3   1      1       1        3    7
4   1      1       2        1    1
5   2      2       1        1    2
6   2      2       1        2    3
7   2      2       2        1    4
8   2      2       2        2    9
9   3      2       1        1    1
10  3      2       2        1    2
11  3      2       3        1    3
12  3      2       4        1    9
13  4      1       1        1    5
14  4      1       2        1    6
15  4      1       2        2    7
16  4      1       3        1    8

id 代表学生 ID,gender 代表性别,item.id 代表学生参加的问题 ID,sequence 是尝试解决问题的序列号,因为学生可能会返回问题并尝试回答再次,time 是每次试验所花费的时间。

在计算时间的平均值时,我需要遵循三个步骤:

(a) 学生对每个问题进行多次试验。我需要计算具有多次试验的每个项目的平均时间。

(b) 然后计算每个id 的总时间平均值。例如,对于id=1,我有两个项目,第一个项目有 3 个试验,第二个项目有 1 个试验。首先我需要通过(5+6+7)/3=6 聚合第一项的时间,所以id=1 有item1 时间6 和item2 时间1。其次,取61 并计算该学生(6+1)/2=3.5 的平均值。

(c) 最后,我想保留数据集中的所有变量。

 data <- data %>%
          group_by(id) %>%
          select(id, gender, item.id, sequence, time) %>%
          summarize(mean.time = mean(time))

我知道了,但显然这只是通过不考虑每次试验的平均值来汇总平均值,这也没有保留所有变量:

> data
# A tibble: 4 x 2
     id mean.time
  <dbl>     <dbl>
1     1      4.75
2     2      4.5 
3     3      3.75
4     4      6.5 

我以为select() 会保留所有变量。

最终的数据集应如下所示:

> data
   id gender item.id sequence time  mean.time
1   1      1       1        1    5    3.5
2   1      1       1        2    6    3.5
3   1      1       1        3    7    3.5
4   1      1       2        1    1    3.5
5   2      2       1        1    2    4.5
6   2      2       1        2    3    4.5
7   2      2       2        1    4    4.5
8   2      2       2        2    5    4.5
9   3      2       1        1    1    3.75
10  3      2       2        1    2    3.75
11  3      2       3        1    3    3.75
12  3      2       4        1    9    3.75
13  4      1       1        1    5    6.5
14  4      1       2        1    6    6.5
15  4      1       2        2    7    6.5
16  4      1       3        1    8    6.5

我使用了dplyr,但打开了任何其他解决方案。 提前致谢!

【问题讨论】:

    标签: r aggregate


    【解决方案1】:

    我们可以使用mutate 代替summarise,因为summarise 返回每​​个组1 行的汇总输出,而mutate 在数据集中创建一个新列

    ...
      %>% 
          mutate(mean.time = mean(time))
    

    如果我们想得到meanmean,那么先按'id'、'item.id'分组,得到mean,再按'id'分组,得到mean unique 个元素

    data %>%
       group_by(id, item.id) %>% 
       mutate(mean.time = mean(time)) %>% 
       group_by(id) %>% 
       mutate(mean.time = mean(unique(mean.time)))
    # A tibble: 16 x 6
    # Groups:   id [4]
    #      id gender item.id sequence  time mean.time
    #   <dbl>  <dbl>   <dbl>    <dbl> <dbl>     <dbl>
    # 1     1      1       1        1     5      3.5 
    # 2     1      1       1        2     6      3.5 
    # 3     1      1       1        3     7      3.5 
    # 4     1      1       2        1     1      3.5 
    # 5     2      2       1        1     2      4.5 
    # 6     2      2       1        2     3      4.5 
    # 7     2      2       2        1     4      4.5 
    # 8     2      2       2        2     9      4.5 
    # 9     3      2       1        1     1      3.75
    #10     3      2       2        1     2      3.75
    #11     3      2       3        1     3      3.75
    #12     3      2       4        1     9      3.75
    #13     4      1       1        1     5      6.5 
    #14     4      1       2        1     6      6.5 
    #15     4      1       2        2     7      6.5 
    #16     4      1       3        1     8      6.5 
    

    或者,我们可以通过match 获取“item.id”的第一个位置,提取“mean.time”并获取mean

    ,而不是创建第二个分组依据
    data %>%
       group_by(id, item.id) %>% 
       mutate(mean.time = mean(time), 
              mean.time = mean(mean.time[match(unique(item.id), item.id)]))
    

    或者使用summarise,然后使用left_join

    data %>%
      group_by(id, item.id) %>%
      summarise(mean.time = mean(time)) %>%
      group_by(id) %>%
      summarise(mean.time = mean(mean.time)) %>%
      right_join(data)
    

    【讨论】:

    • 我明白了。那么,我怎样才能考虑到序列内的平均值呢?你能看看(a)(b)之后的例子吗?
    • 实际上还有一件事。当您使用unique(mean.time) 时,它会消除重复值。如果某些问题具有相同的mean.time 值怎么办?
    • @amisos55 它将返回相同的值,即mean(c(5, 5))
    • 假设mean.time &lt;- c(2,3,4,4,5,5,6)。取平均值mean(mean.time)= 4.14,但不包括重复值,取平均值unique() 它给出mean(unique(mean.time)) = 4。没有unique(),有没有办法做到这一点?
    • @amisos55 在这种情况下,连接解决方​​案应该可以工作,对吧?
    猜你喜欢
    • 2015-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-08
    • 1970-01-01
    • 2020-05-22
    相关资源
    最近更新 更多