Goal: Create a table that includes the M, SD, Min, and Max values for all six variables in your analyses (DERS, sens, and the 4 ITSEA variables).

library(haven)
baby2.0cor <- read_sav("TheaWulff_Dataset_LR.sav")

library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
baby2.0cor <- baby2.0cor %>%
  rename(DERS = DERStotmA,
         SensComposite = OverallSensComposite,
         ITSEA_exter = ITSEAextmD1,
         ITSEA_inter = ITSEAintmD1,
         ITSEA_dysreg = ITSEAdysmD1,
         ITSEA_comp = ITSEAcompmD1)
# Exclude values of 999 in the 'DERS_average' variable
baby2.0cor_filtered <- baby2.0cor[baby2.0cor$DERS_average != 999, ]

variables <- c("DERS_average", "SensComposite", "ITSEA_exter", "ITSEA_inter", "ITSEA_dysreg", "ITSEA_comp")

summary_stats <- sapply(baby2.0cor_filtered[variables], function(x) c(M = mean(x, na.rm = TRUE),
                                                                      SD = sd(x, na.rm = TRUE),
                                                                      Min = min(x, na.rm = TRUE),
                                                                      Max = max(x, na.rm = TRUE)))

# Convert the result to a data frame
summary_stats_df <- as.data.frame(t(summary_stats))

# Rename rows and columns
rownames(summary_stats_df) <- c("DERS", "Parenting Sensitivity", "ITSEA Externalizing", "ITSEA Internalizing", "ITSEA Dysregulation", "ITSEA Competence")
colnames(summary_stats_df) <- c("Mean", "Standard Deviation", "Minimum", "Maximum")

# View the summary statistics table
print(summary_stats_df)
##                            Mean Standard Deviation   Minimum  Maximum
## DERS                  2.2251565          0.7025168 1.0000000 4.583333
## Parenting Sensitivity 3.5788804          0.7126563 1.7500000 4.812500
## ITSEA Externalizing   0.4283293          0.2204725 0.0000000 1.292929
## ITSEA Internalizing   0.5426754          0.2181032 0.1250000 1.300000
## ITSEA Dysregulation   0.4153161          0.2376254 0.0000000 1.356944
## ITSEA Competence      1.2651528          0.2707037 0.3916667 1.925000
# Export the table as a CSV file
write.csv(summary_stats_df, "summary_stats.csv", row.names = TRUE)

# Load necessary library to download as a PNG instead of a .csv
library(gridExtra) 
## 
## Attaching package: 'gridExtra'
## The following object is masked from 'package:dplyr':
## 
##     combine
# Open a PNG file to save the image
png("summary_stats_table.png", width = 800, height = 600)

# Create the table and render it as an image
grid.table(summary_stats_df)

# Close the PNG device to save the image
dev.off()
## quartz_off_screen 
##                 2