# load 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(ggplot2)
library(papaja)
## Loading required package: tinylabels
library(glue)
# read in data being used
study_1 <- 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.
study_2 <- 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.
figure_1_historgram <- hist(study_1$SCdiff,
main = "Distribution of Social Connectedness Difference Scores",
xlab = "Social Connectedness Difference Score (T2-T1)",
xlim = c(-3,3)
)
figure_1_ggplot <- ggplot(data = study_1, mapping = aes(x = SCdiff))+
geom_histogram(bins = 12, colour = "black", fill = "grey")+
ylim(0, 150)+
# xlim(-3,3)+
labs(title = "Distribution of Social Connectedness Difference Scores",
x = "Social Connectedness Difference Score (T2-T1)",
y = "Frequency") +
theme_apa()
print(figure_1_ggplot)
# Solution 1
# Used paste() to stick the output into one cell of the table
table_1 <- study_1 %>% 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 (T2-T1)" = 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), ")")
)
# To add a row name, I converted the tibble into a dataframe (if it was changed back to a tibble, the row name won't appear)
table_1 <- as.data.frame(table_1)
rownames(table_1) <- paste0("Mean (SD)")
print(table_1)
## T1 Lethargy T2 Lethargy Lethargy diff (T2-T1)
## Mean (SD) 2.6 ( 1.16 ) 3.16 ( 1.27 ) 0.56 ( 1.33 )
## T1 Social connectedness T2 Social connectedness Connectedness diff
## Mean (SD) 4.11 ( 0.88 ) 3.97 ( 0.85 ) -0.14 ( 0.71 )
## Extraversion
## Mean (SD) 4.17 ( 1.01 )
# Solution 2
# table_1 %>% mutate(new = glue("{t1lethmean} ({t1lethsd})"))
#initally, I tried to just write it out as a string but that didn't seem intuitive
# T2_Lethargy = round(mean(LETHAVERAGE.T2), 2),
# T2_Lethargy_sd = round(sd(LETHAVERAGE.T2), 2),
# Lethargy_diff = round(mean(LethDiff), 2),
# Lethargy_diff_sd = round(sd(LethDiff), 2),
# T1_SC = round(mean(SCAVERAGE.T1), 2),
# T1_SC_sd = round(sd(SCAVERAGE.T1), 2),
# T2_SC = round(mean(SCAVERAGE.T2), 2),
# T2_SC_sd = round(sd(SCAVERAGE.T2), 2),
# SC_diff = round(mean(SCdiff), 2),
# SC_diff_sd = round(sd(SCdiff), 2),
# Extraversion = round(mean(EXTRAVERSION), 2),
# Extraversion_sd = round(sd(EXTRAVERSION), 2)) %>%
# mutate(T1_Lethargy = "2.60 (1.16)",
# T2_Lethargy = "3.16 (1.27)",
# Lethargy_diff = "0.56 (1.33)",
# T1_SC = "4.11 (0.88)",
# T2_SC = "3.97 (0.85)",
# SC_diff = "-0.14 (0.71)",
# Extraversion = "4.17 (1.01)") %>%
# select(-ends_with("sd"))
# meanT1Lethargy <- study_1 %>% summarise(T1_Lethargy = round(mean(LETHAVERAGE.T1), 2))
#
# sdT1Lethargy <- study_1 %>% summarise(T1_Lethargy_sd = round(sd(LETHAVERAGE.T1), 2))
#
# cbind(meanT1Lethargy, sdT1Lethargy)
# figure_2_histogram <- hist.default(study_2$BMPN_Diff)
#
# figure_2_ggplot <- ggplot(data = study_2, mapping = aes(x = BMPN_Diff))+
# geom_histogram(binwidth = 1,bins = 14, xlim = (-4:4))
#
# print(figure_2_ggplot)
table_3 <- study_2 %>% summarise("T1 Life Satisfaction" = paste(round(mean(T1SWLS), 2),"(", round(sd(T1SWLS), 2),")"),
"T2 Life Satisfaction" = paste(round(mean(T2SWLS), 2),"(", round(sd(T2SWLS), 2),")"),
"Life Satisfaction change (T2-T1)" = paste(round(mean(SWLS_Diff), 2),"(", round(sd(SWLS_Diff), 2),")"),
"T1 Relatedness" = paste(round(mean(T1BMPN), 2),"(", round(sd(T1BMPN), 2),")"),
"T2 Relatedness" = paste(round(mean(T2BMPN), 2),"(", round(sd(T2BMPN), 2),")"),
"Relatedness change (T2-T1)" = paste(round(mean(BMPN_Diff), 2),"(", round(sd(BMPN_Diff), 2),")"),
"T1 Loneliness" = paste(round(mean(T1Lonely), 2),"(", round(sd(T1Lonely), 2),")"),
"T2 Loneliness" = paste(round(mean(T2Lonely), 2),"(", round(sd(T2Lonely), 2),")"),
"Loneliness change (T2-T1)" = paste(round(mean(Lonely_Diff), 2),"(", round(sd(Lonely_Diff), 2),")"),
"T1 Extraversion" = paste(round(mean(T1Extraversion), 2),"(", round(sd(T1Extraversion), 2),")"),
)
# To add a row name, I converted the tibble into a dataframe
table_3 <- as.data.frame(table_3)
rownames(table_3) <- paste0("Mean (SD)")
print(table_3)
## T1 Life Satisfaction T2 Life Satisfaction
## Mean (SD) 3.97 ( 1.53 ) 3.99 ( 1.45 )
## Life Satisfaction change (T2-T1) T1 Relatedness T2 Relatedness
## Mean (SD) 0.02 ( 0.88 ) 4.92 ( 1.09 ) 4.91 ( 1.14 )
## Relatedness change (T2-T1) T1 Loneliness T2 Loneliness
## Mean (SD) -0.01 ( 1.11 ) 2.12 ( 0.65 ) 2.06 ( 0.62 )
## Loneliness change (T2-T1) T1 Extraversion
## Mean (SD) -0.06 ( 0.4 ) 3.9 ( 0.79 )