Calculate the average value for each object for different conditions

Although I know how to calculate the average value in R, I cannot figure out how to do this for different conditions. The other posts I read were not so 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 dataset above, I would like to get the average value for each bin ( Bin0 , Bin1 , Bin2 ) for each object for each TrialType for Target/dictractor separately. For example, for subject 1, I would like to calculate the average for each bin for TrialType 2C in the Target condition, then the average for each bin for TrialType 2E in the Target condition, then for each bin for TrialType 2C in the Distractor condition and TrialType 2E in Distractor .

+6
source share
2 answers

Here is my attempt. You can use group_by() and get combinations of id , target and trial . For each combination, you want to get the average for bin0-bin3. In this case, you can do 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 

Alternatively, you can try the data.table package data.table perform 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 
+3
source

One way with the dplyr package:

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) 

Decision

 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 
+2
source

Source: https://habr.com/ru/post/981848/


All Articles