# codebook_generation_01.R
#
# Code generation example
#
# Input:  results/data_rdm-ms-ss2024_cleaned.RData
# Output: 
#
# created: 2024-06-04

# setwd("C:/Users/nwickelmaier/Nextcloud/Documents/teaching/iwm/data_management/03_data_organisation/example/")

load("data/results/data_rdm-ms-ss2024_cleaned.RData")

codebook <- data.frame(var_name = names(dat),
                       var_text = c("Response Id", "Please indicate your sex.",
                       "How old are you? Please enter your age in years.",
                       "Have you ever published data in a repository?",
                       "How many of your data sets have you published so far?",
                       "All my analyses are preregistered",
                       "Sharing my data is very important to me",
                       "I invest more time in research data management than my colleagues",
                       "I think research data management is overrated",
                       "Sharing data is bad scientific practice",
                       "What is your current career level?",
                       "How long have you been working in science (in years)?")

)

codebook$type <- sapply(dat, class)
codebook$n <- sapply(dat, length)
codebook$mean <- sapply(dat, function(x) ifelse(is.numeric(x), mean(x, na.rm = TRUE), NA))
codebook$sd <- sapply(dat, function(x) ifelse(is.numeric(x), sd(x), NA))

write.table(codebook,
            file = "data/codebook/codebook_01.csv",
            na = "",
            sep = ";",
            quote = FALSE) 

openxlsx::write.xlsx(codebook, file = "data/codebook/codebook_01.xlsx")

