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

35 lines
974 B
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 (if present)
df <- df %>% select(-any_of("demo_f"))
str(df)
# Coerce to numeric where possible (without breaking non-numeric)
df_num <- df %>%
mutate(across(everything(), ~ suppressWarnings(as.numeric(.))))
# Compute count and proportion correct (value == 1) per variable
descriptives <- df_num %>%
pivot_longer(everything(), names_to = "variable", values_to = "value") %>%
summarise(
n_total = sum(!is.na(value)),
n_correct = sum(value == 1, na.rm = TRUE),
prop_correct = ifelse(n_total > 0, n_correct / n_total, NA_real_),
.by = "variable"
) %>%
arrange(variable)
# View
print(descriptives, n = Inf)
# Optionally save
# readr::write_csv(descriptives, "exp1_TF_descriptives.csv")