# R for Psychological Research, Lesson 7: Summarise groups # Code from the lesson page, in order. Run it line by line with Ctrl+Enter (Cmd+Enter on a Mac). # Lines that start with # are comments: R ignores them. # ---- Set up ---- library(tidyverse) students <- read_csv("data/students.csv", show_col_types = FALSE) # ---- summarise(): many rows to one ---- students %>% summarise(n = n(), mean_exam = mean(ExamScore), sd_exam = sd(ExamScore)) # ---- group_by(): one summary per group ---- students %>% group_by(Motivation) %>% summarise(n = n(), mean_exam = mean(ExamScore), mean_study = mean(StudyHours, na.rm = TRUE)) # ---- Put groups in order ---- students <- students %>% mutate(Motivation = factor(Motivation, levels = c("Low", "Medium", "High"))) students %>% group_by(Motivation) %>% summarise(n = n(), mean_exam = mean(ExamScore)) # ---- Two groupings at once ---- students %>% group_by(Workshop, Gender) %>% summarise(n = n(), mean_change = mean(Wellbeing_T2 - Wellbeing_T1), .groups = "drop")