options(scipen = 999)
setwd("C:/Users/irina/Documents/DND/EOHI/eohi1")

# Load the data
exp1_data <- read.csv("exp1.csv")

# Define all NPastDiff and NFutDiff variables
all_diff_vars <- c(
  "NPastDiff_pref_read", "NPastDiff_pref_music", "NPastDiff_pref_tv", "NPastDiff_pref_nap", "NPastDiff_pref_travel",
  "NPastDiff_pers_extravert", "NPastDiff_pers_critical", "NPastDiff_pers_dependable", "NPastDiff_pers_anxious", "NPastDiff_pers_complex",
  "NPastDiff_val_obey", "NPastDiff_val_trad", "NPastDiff_val_opinion", "NPastDiff_val_performance", "NPastDiff_val_justice",
  "NPastDiff_life_ideal", "NPastDiff_life_excellent", "NPastDiff_life_satisfied", "NPastDiff_life_important", "NPastDiff_life_change",
  "NFutDiff_pref_read", "NFutDiff_pref_music", "NFutDiff_pref_tv", "NFutDiff_pref_nap", "NFutDiff_pref_travel",
  "NFutDiff_pers_extravert", "NFutDiff_pers_critical", "NFutDiff_pers_dependable", "NFutDiff_pers_anxious", "NFutDiff_pers_complex",
  "NFutDiff_val_obey", "NFutDiff_val_trad", "NFutDiff_val_opinion", "NFutDiff_val_performance", "NFutDiff_val_justice",
  "NFutDiff_life_ideal", "NFutDiff_life_excellent", "NFutDiff_life_satisfied", "NFutDiff_life_important", "NFutDiff_life_change"
)

# Define DGEN variables to average
dgen_vars <- c("pastPref_DGEN", "pastPers_DGEN", "pastVal_DGEN", "pastLife_DGEN",
               "futPref_DGEN", "futPers_DGEN", "futVal_DGEN", "futLife_DGEN")

# Calculate domain_mean as average of all 40 variables
exp1_data$domain_mean <- rowMeans(exp1_data[, all_diff_vars], na.rm = TRUE)

# Calculate DGEN_mean as average of all 8 DGEN variables
exp1_data$DGEN_mean <- rowMeans(exp1_data[, dgen_vars], na.rm = TRUE)

# Save the updated data
write.csv(exp1_data, "exp1.csv", row.names = FALSE)

# Display summary of the calculated domain_mean
cat("Domain mean summary (average of all 40 NPastDiff and NFutDiff variables):\n")
summary(exp1_data$domain_mean)

# Show first few rows to verify calculations
cat("\nFirst 5 rows of calculated domain_mean:\n")
print(exp1_data[1:5, "domain_mean"])
