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

39 lines
1.0 KiB
R

library(tidyverse)
setwd("C:/Users/irina/Documents/DND/EOHI/eohi1")
# Read data
data <- read.csv("exp1.csv")
# Select variables ending exactly with _T or _F
df <- data %>% select(matches("(_T|_F)$"))
# Remove demo_f variable
df <- df %>% select(-demo_f)
str(df)
# Coerce to numeric where possible (without breaking non-numeric)
df_tf_num <- df %>%
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")