#load the packages

library(tidyverse) 
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.5.1     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.1
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(knitr) #for kable()
library (DT) #for datatable()

#read in the data

study1 <- read_csv(file = "Study 1 data.csv")
## Rows: 467 Columns: 15
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr  (2): Gender, Age
## dbl (13): Participant ID, LETHAVERAGE.T1, LETHAVERAGE.T2, LethDiff, SCAVERAG...
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
study2 <- read_csv(file = "Study 2 data.csv")
## Rows: 336 Columns: 17
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr  (3): Gender, Ethnicity, Country
## dbl (14): Participant_ID, Age, T1Extraversion, T1SWLS, T2SWLS, SWLS_Diff, T1...
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.

#Attempt 1 to summarise the SD and Mean

study1table <- study1 %>% summarise(
  T1Lethargy = round(mean(LETHAVERAGE.T1),2),
  T2Lethargy = round(mean(LETHAVERAGE.T2),2),
  LethargyDiff = round(mean(LethDiff),2),
  T1SocialConnectedness = round(mean(SCAVERAGE.T1),2),
  T2SocialConnectedness = round(mean(SCAVERAGE.T2),2),
  ConnectednessDiff = round(mean(SCdiff),2),
  Extraversion = round(mean(EXTRAVERSION),2),
  T1Lethargy_sd = round(sd(LETHAVERAGE.T1),2),
  T2Lethargy_sd = round(sd(LETHAVERAGE.T2),2),
  LethargyDiff_sd = round(sd(LethDiff),2),
  T1SocialConnectedness_sd = round(sd(SCAVERAGE.T1),2),
  T2SocialConnectedness_sd = round(sd(SCAVERAGE.T2),2),
  ConnectednessDiff_sd = round(sd(SCdiff),2),
  Extraversion_sd = round(sd(EXTRAVERSION),2)
)

#very tedious and long method 

#Attempt 2 to summarise the SD and Mean. The 2 at the end of each line of code is for 2dp.

study1tablenew <- study1 %>%  
  summarise(
  "T1 Lethargy" = paste(round(mean(LETHAVERAGE.T1),2),"(", round(sd(LETHAVERAGE.T1), 2),")"),
  "T2 Lethargy" = paste(round(mean(LETHAVERAGE.T2),2),"(", round(sd(LETHAVERAGE.T2), 2),")"),
  "Lethargy Diff" = paste(round(mean(LethDiff),2),"(", round(sd(LethDiff), 2),")"),
  "T1 Social Connectedness" = paste(round(mean(SCAVERAGE.T1),2),"(", round(sd(SCAVERAGE.T1), 2),")"),
  "T2 Social Connectedness" = paste(round(mean(SCAVERAGE.T2),2),"(", round(sd(SCAVERAGE.T2), 2),")"),
  "Connectedness Diff" = paste(round(mean(SCdiff),2),"(", round(sd(SCdiff), 2),")"),
  "Extraversion" = paste(round(mean(EXTRAVERSION),2),"(", round(sd(EXTRAVERSION), 2),")")
  ) 

#more concise and neat method

#Getting the table - Attempt 1 - using the print()

study1tableprint <- study1 %>% summarise(
  T1Lethargy = round(mean(LETHAVERAGE.T1),2),
  T2Lethargy = round(mean(LETHAVERAGE.T2),2),
  LethargyDiff = round(mean(LethDiff),2),
  T1SocialConnectedness = round(mean(SCAVERAGE.T1),2),
  T2SocialConnectedness = round(mean(SCAVERAGE.T2),2),
  ConnectednessDiff = round(mean(SCdiff),2),
  Extraversion = round(mean(EXTRAVERSION),2),
  T1Lethargy_sd = round(sd(LETHAVERAGE.T1),2),
  T2Lethargy_sd = round(sd(LETHAVERAGE.T2),2),
  LethargyDiff_sd = round(sd(LethDiff),2),
  T1SocialConnectedness_sd = round(sd(SCAVERAGE.T1),2),
  T2SocialConnectedness_sd = round(sd(SCAVERAGE.T2),2),
  ConnectednessDiff_sd = round(sd(SCdiff),2),
  Extraversion_sd = round(sd(EXTRAVERSION),2)
)

print(study1tableprint) 
## # A tibble: 1 × 14
##   T1Lethargy T2Lethargy LethargyDiff T1SocialConnectedness T2SocialConnectedness
##        <dbl>      <dbl>        <dbl>                 <dbl>                 <dbl>
## 1        2.6       3.16         0.56                  4.11                  3.97
## # ℹ 9 more variables: ConnectednessDiff <dbl>, Extraversion <dbl>,
## #   T1Lethargy_sd <dbl>, T2Lethargy_sd <dbl>, LethargyDiff_sd <dbl>,
## #   T1SocialConnectedness_sd <dbl>, T2SocialConnectedness_sd <dbl>,
## #   ConnectednessDiff_sd <dbl>, Extraversion_sd <dbl>

#Getting the table - Attempt 2 - using datatable () - this makes an interactive table

study1tabledatatable <- study1 %>%  
  summarise(
  "T1 Lethargy" = paste(round(mean(LETHAVERAGE.T1),2),"(", round(sd(LETHAVERAGE.T1), 2),")"),
  "T2 Lethargy" = paste(round(mean(LETHAVERAGE.T2),2),"(", round(sd(LETHAVERAGE.T2), 2),")"),
  "Lethargy Diff" = paste(round(mean(LethDiff),2),"(", round(sd(LethDiff), 2),")"),
  "T1 Social Connectedness" = paste(round(mean(SCAVERAGE.T1),2),"(", round(sd(SCAVERAGE.T1), 2),")"),
  "T2 Social Connectedness" = paste(round(mean(SCAVERAGE.T2),2),"(", round(sd(SCAVERAGE.T2), 2),")"),
  "Connectedness Diff" = paste(round(mean(SCdiff),2),"(", round(sd(SCdiff), 2),")"),
  "Extraversion" = paste(round(mean(EXTRAVERSION),2),"(", round(sd(EXTRAVERSION), 2),")")
  )

datatable(study1tabledatatable) 

#Getting the table - Attempt 3 - using kable()

study1tablekable <- study1 %>%  
  summarise(
  "T1 Lethargy" = paste(round(mean(LETHAVERAGE.T1),2),"(", round(sd(LETHAVERAGE.T1), 2),")"),
  "T2 Lethargy" = paste(round(mean(LETHAVERAGE.T2),2),"(", round(sd(LETHAVERAGE.T2), 2),")"),
  "Lethargy Diff" = paste(round(mean(LethDiff),2),"(", round(sd(LethDiff), 2),")"),
  "T1 Social Connectedness" = paste(round(mean(SCAVERAGE.T1),2),"(", round(sd(SCAVERAGE.T1), 2),")"),
  "T2 Social Connectedness" = paste(round(mean(SCAVERAGE.T2),2),"(", round(sd(SCAVERAGE.T2), 2),")"),
  "Connectedness Diff" = paste(round(mean(SCdiff),2),"(", round(sd(SCdiff), 2),")"),
  "Extraversion" = paste(round(mean(EXTRAVERSION),2),"(", round(sd(EXTRAVERSION), 2),")")
  )

table1 <- as.data.frame(study1tablekable)
rownames(table1) <- "Mean (SD)"  
  
kable(table1)
T1 Lethargy T2 Lethargy Lethargy Diff T1 Social Connectedness T2 Social Connectedness Connectedness Diff Extraversion
Mean (SD) 2.6 ( 1.16 ) 3.16 ( 1.27 ) 0.56 ( 1.33 ) 4.11 ( 0.88 ) 3.97 ( 0.85 ) -0.14 ( 0.71 ) 4.17 ( 1.01 )

#Getting the table - Attempt 4 - making the 2.6 into 2.60 Had to change the round() function into the sprintf() function. In this code, sprintf(“%.2f”, value) ensures that all numerical values are formatted to two decimal places with trailing zeros where necessary.

study1tablekable2 <- study1 %>%  
  summarise(
    "T1 Lethargy" = paste(sprintf("%.2f", mean(LETHAVERAGE.T1)), "(", sprintf("%.2f", sd(LETHAVERAGE.T1)), ")"),
    "T2 Lethargy" = paste(sprintf("%.2f", mean(LETHAVERAGE.T2)), "(", sprintf("%.2f", sd(LETHAVERAGE.T2)), ")"),
    "Lethargy Diff (T2-T1)" = paste(sprintf("%.2f", mean(LethDiff)), "(", sprintf("%.2f", sd(LethDiff)), ")"),
    "T1 Social Connectedness" = paste(sprintf("%.2f", mean(SCAVERAGE.T1)), "(", sprintf("%.2f", sd(SCAVERAGE.T1)), ")"),
    "T2 Social Connectedness" = paste(sprintf("%.2f", mean(SCAVERAGE.T2)), "(", sprintf("%.2f", sd(SCAVERAGE.T2)), ")"),
    "Connectedness Diff (T2-T1)" = paste(sprintf("%.2f", mean(SCdiff)), "(", sprintf("%.2f", sd(SCdiff)), ")"),
    "Extraversion" = paste(sprintf("%.2f", mean(EXTRAVERSION)), "(", sprintf("%.2f", sd(EXTRAVERSION)), ")")
  )

table2 <- as.data.frame(study1tablekable2)
rownames(table2) <- "Mean (SD)" 
  
kable(table2)
T1 Lethargy T2 Lethargy Lethargy Diff (T2-T1) T1 Social Connectedness T2 Social Connectedness Connectedness Diff (T2-T1) Extraversion
Mean (SD) 2.60 ( 1.16 ) 3.16 ( 1.27 ) 0.56 ( 1.33 ) 4.11 ( 0.88 ) 3.97 ( 0.85 ) -0.14 ( 0.71 ) 4.17 ( 1.01 )