计算不同条件下每个受试者的平均值

时间:2015-02-02 00:02:53

标签: r mean trial subject

虽然我知道如何计算R中的均值,但我似乎无法理解如何在不同条件下进行计算。我读过的其他帖子并不复杂。

>      Target/dictractor    TrialType    Bin0    Bin1    Bin2    Bin3
1          Target              2C           3       0       2       0
1          Target              2C           2       0       3       0
1          Target              2E           0       1       1       2
1          Target              2E           0       0       0       0
1        Distractor            2C           0       3       0       1
1        Distractor            2C           0       0       0       0
1        Distractor            2E           0       0       1       0
1        Distractor            2E           0       0       0       3
2          Target              2C           1       1       0       1
2          Target              2C           2       0       0       2
2        Distractor            2E           0       0       0       0
2        Distractor            2E           0       0       0       0

根据上面的数据集,我想得到Bin0 Bin1的每个主题的每个分档(Bin2TrialTypeTarget/dictractor)的均值1}}分开。例如,对于主题1,我想在TrialType条件下计算2C Target的每个分档的平均值,然后计算TrialType的每个分箱的均值2E条件下的TargetTrialType条件中的2C Distractor以及TrialType 2E {1}}。

2 个答案:

答案 0 :(得分:3)

这是我的尝试。您可以使用group_by()获取idtargettrial的组合。对于每个组合,您希望获得bin0-bin3的均值。在这种情况下,您可以使用summarise_each()进行此操作。

mydf <- data.frame(id = c(1,1,1,1,1,1,1,1,2,2,2,2),
                   target = c("target", "target", "target", "target", "distractor",
                        "distractor", "distractor", "distractor",
                        "target", "target", "distractor", "distractor"),
                   trial = c("2c", "2c", "2e", "2e", "2c", "2c", "2e", "2e",
                             "2c", "2c", "2e", "2e"),
                   bin0 = c(3,2,0,0,0,0,0,0,1,2,0,0),
                   bin1 = c(0,0,1,0,3,0,0,0,1,0,0,0),
                   bin2 = c(2,3,1,0,0,0,1,0,0,0,0,0),
                   bin3 = c(0,0,2,0,1,0,0,3,1,2,0,0),
                   stringsAsFactors = FALSE)

library(dplyr)          
group_by(mydf, id, target, trial) %>%
summarise_each(funs(mean(., na.rm = TRUE)), bin0:bin3)

#  id     target trial bin0 bin1 bin2 bin3
#1  1 distractor    2c  0.0  1.5  0.0  0.5
#2  1 distractor    2e  0.0  0.0  0.5  1.5
#3  1     target    2c  2.5  0.0  2.5  0.0
#4  1     target    2e  0.0  0.5  0.5  1.0
#5  2 distractor    2e  0.0  0.0  0.0  0.0
#6  2     target    2c  1.5  0.5  0.0  1.5

或者,您可以尝试data.table包来执行相同的操作。

foo <- setDT(mydf)[, lapply(.SD, mean), by = list(id, target, trial)]
print(foo)

#   id     target trial bin0 bin1 bin2 bin3
#1:  1     target    2c  2.5  0.0  2.5  0.0
#2:  1     target    2e  0.0  0.5  0.5  1.0
#3:  1 distractor    2c  0.0  1.5  0.0  0.5
#4:  1 distractor    2e  0.0  0.0  0.5  1.5
#5:  2     target    2c  1.5  0.5  0.0  1.5
#6:  2 distractor    2e  0.0  0.0  0.0  0.0

答案 1 :(得分:2)

dplyr包的一种方法是:

数据

df <- read.table(header=T,text='    Subject Target/dictractor    TrialType    Bin0    Bin1    Bin2    Bin3
1          Target              2C           3       0       2       0
1          Target              2C           2       0       3       0
1          Target              2E           0       1       1       2
1          Target              2E           0       0       0       0
1        Distractor            2C           0       3       0       1
1        Distractor            2C           0       0       0       0
1        Distractor            2E           0       0       1       0
1        Distractor            2E           0       0       0       3
2          Target              2C           1       1       0       1
2          Target              2C           2       0       0       2
2        Distractor            2E           0       0       0       0
2        Distractor            2E           0       0       0       0', stringsAsFactors=F)

<强>解决方案

df %>%
  group_by(Subject, Target.dictractor,TrialType) %>%
  summarise(mean_Bin0=mean(Bin0),
            mean_Bin1=mean(Bin1),
            mean_Bin2=mean(Bin2),
            mean_Bin3=mean(Bin3))

<强>输出

Source: local data frame [6 x 7]
Groups: Subject, Target.dictractor

  Subject Target.dictractor TrialType mean_Bin0 mean_Bin1 mean_Bin2 mean_Bin3
1       1        Distractor        2C       0.0       1.5       0.0       0.5
2       1        Distractor        2E       0.0       0.0       0.5       1.5
3       1            Target        2C       2.5       0.0       2.5       0.0
4       1            Target        2E       0.0       0.5       0.5       1.0
5       2        Distractor        2E       0.0       0.0       0.0       0.0
6       2            Target        2C       1.5       0.5       0.0       1.5