Although I know how to compute the mean in R, I can't seem to understand how to do it for different conditions. Other posts that I have read were not as complicated.
> 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
Given the data set above I would like to get the mean of each bin (Bin0
, Bin1
, Bin2
) for each subject for each TrialType
for Target/dictractor
separately. For example, for subject 1 I would like to compute the mean of each bin, for TrialType
2C
, in a Target
condition, then the mean for each bin for TrialType
2E
in a Target
condition, then mean for each bin for TrialType
2C
in Distractor
condition and TrialType
2E
in Distractor
.
Here is my attempt. You can use group_by()
and get the combinations of id
, target
and trial
. For each combination, you want to get mean for bin0-bin3. This is something you can do with summarise_each()
in this case.
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
Alternatively, you can try the data.table
package to do the same operation.
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
One way with the dplyr
package is:
Data
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)
Solution
df %>%
group_by(Subject, Target.dictractor,TrialType) %>%
summarise(mean_Bin0=mean(Bin0),
mean_Bin1=mean(Bin1),
mean_Bin2=mean(Bin2),
mean_Bin3=mean(Bin3))
Output
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With