I have a dataframe that looks like this:
example <- data.frame(
Subject = c(rep(101, 8),
rep(102, 8)),
Run = c(1,1,1,1,2,2,2,2,1,1,1,1,2,2,2,2),
Conditions = c(rep(c('SIN', 'SIN', 'SRN', 'SRN'), 4)),
Score = c(1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4)
)
How can I find the mean of Score by Subject and Conditions? The output should look like this:
solution <- data.frame(
Subject = c(101, 101, 102, 102),
Conditions = c(rep(c('SIN', 'SRN'),2)),
Score = c(1.5,1.5,3.5,3.5)
)
Thank you!
>Solution :
Update after clarification:
library(dplyr) #>1.1.0
example %>%
summarise(Mean_Score = mean(Score, na.rm = TRUE), .by=c(Subject, Conditions))
Subject Conditions Mean_Score
1 101 SIN 1.5
2 101 SRN 1.5
3 102 SIN 3.5
4 102 SRN 3.5
First answer:
First we calculate the mean after grouping by Subject, Run, Conditions then we ungroup and group by Subject to filter for the first and last entry and finally we remove column Run:
library(dplyr) #>1.1.0
example %>%
summarise(Mean_Score = mean(Score, na.rm = TRUE), .by=c(Subject, Run, Conditions)) %>%
filter(row_number()==1 | row_number()==n(), .by = Subject) %>%
select(-Run)
Subject Conditions Mean_Score
1 101 SIN 1
2 101 SRN 2
3 102 SIN 3
4 102 SRN 4