# R for Psychological Research, Lesson 10: Describe data
# 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)


# ---- The middle: mean or median? ----

mean(students$Sleep, na.rm = TRUE)
median(students$Sleep, na.rm = TRUE)


# ---- The spread ----

sd(students$Sleep, na.rm = TRUE)
min(students$Sleep, na.rm = TRUE)
max(students$Sleep, na.rm = TRUE)
quantile(students$Sleep, probs = c(0.25, 0.75), na.rm = TRUE)
IQR(students$Sleep, na.rm = TRUE)


# ---- One table per group ----

students %>%
  group_by(Gender) %>%
  summarise(n = n(),
            missing = sum(is.na(Sleep)),
            mean = mean(Sleep, na.rm = TRUE),
            sd = sd(Sleep, na.rm = TRUE),
            median = median(Sleep, na.rm = TRUE),
            IQR = IQR(Sleep, na.rm = TRUE))


# ---- Everything at once: describe() ----

library(psych)
students %>%
  select(Sleep, Stress, ExamScore) %>%
  describe()
