Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Finding the mean of a column after grouping by multiple other columns in R

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!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading