eohi/.history/eohi1/descriptives - gen knowledge questions_20250918115553.r
2025-12-23 15:47:09 -05:00

32 lines
975 B
R

library(tidyverse)
# Read data
df <- readr::read_csv("exp1.csv", show_col_types = FALSE)
# Select variables ending exactly with _T or _F
df_tf <- df %>% select(matches("(_T|_F)$"))
# Coerce to numeric where possible (without breaking non-numeric)
df_tf_num <- df_tf %>%
mutate(across(everything(), ~ suppressWarnings(as.numeric(.))))
# Compute descriptives per variable
descriptives <- df_tf_num %>%
pivot_longer(everything(), names_to = "variable", values_to = "value") %>%
summarise(
n = sum(!is.na(value)),
missing = sum(is.na(value)),
mean = mean(value, na.rm = TRUE),
sd = sd(value, na.rm = TRUE),
median = median(value, na.rm = TRUE),
min = suppressWarnings(min(value, na.rm = TRUE)),
max = suppressWarnings(max(value, na.rm = TRUE)),
.by = "variable"
) %>%
arrange(variable)
# View
print(descriptives, n = Inf)
# Optionally save
# readr::write_csv(descriptives, "exp1_TF_descriptives.csv")