eohi/eohi2/datap 15 - education recoded ordinal 3.r
2026-01-22 17:55:35 -05:00

38 lines
1.3 KiB
R

options(scipen = 999)
setwd("C:/Users/irina/Documents/DND/EOHI/eohi2")
data <- read.csv("eohi2.csv")
# Check the levels of the demo_edu variable
print(levels(factor(data$demo_edu)))
# Also show the unique values and their frequencies
print("\nUnique values and frequencies:")
print(table(data$demo_edu, useNA = "ifany"))
# Recode demo_edu into 3 ordinal levels
data$edu3 <- NA
# HS_TS: High School and Trade School
data$edu3[data$demo_edu %in% c("High School (or equivalent)", "Trade School (non-military)")] <- "HS_TS"
# C_Ug: College and University - Undergraduate
data$edu3[data$demo_edu %in% c("College Diploma/Certificate", "University - Undergraduate")] <- "C_Ug"
# grad_prof: University - Graduate, University - PhD, and Professional Degree
data$edu3[data$demo_edu %in% c("University - Graduate (Masters)", "University - PhD", "Professional Degree (ex. JD/MD)")] <- "grad_prof"
# Convert to ordered factor
data$edu3 <- factor(data$edu3,
levels = c("HS_TS", "C_Ug", "grad_prof"),
ordered = TRUE)
# Check the recoded variable
print(table(data$edu3, useNA = "ifany"))
# Verify the recoding
print(table(data$demo_edu, data$edu3, useNA = "ifany"))
# Save the updated dataset with the new edu3 variable
write.csv(data, "eohi2.csv", row.names = FALSE)