Attempting to recreate Table 2 for means and SDs (excluding t-values, and p-values):
Load relevant packages
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.1 ──
## ✓ ggplot2 3.3.4 ✓ purrr 0.3.4
## ✓ tibble 3.1.2 ✓ dplyr 1.0.7
## ✓ tidyr 1.1.3 ✓ stringr 1.4.0
## ✓ readr 1.4.0 ✓ forcats 0.5.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(dplyr)
Calculating mean (and SD) baseline Race bias
BaselineRace <- cleandata %>% #creating new data "BaselineRace" from "cleandata"
select(base_IAT_race) %>% #select variable "base_IAT_race" from "cleandata"
summarise(BaselineRaceMean = mean(base_IAT_race), BaselineRaceSD = sd(base_IAT_race)) #calculate mean and SD for selected variable
## Error in select(., base_IAT_race): object 'cleandata' not found
print(BaselineRace)
## Error in print(BaselineRace): object 'BaselineRace' not found
Calculating mean (and SD) baseline gender bias
BaselineGender <- cleandata %>% #creating new data "BaselineRace" from "cleandata"
select(base_IAT_gen) %>% #select variable "base_IAT_gen" from "cleandata"
summarise(BaselineGenderMean = mean(base_IAT_gen), BaselineGenderSD = sd(base_IAT_gen)) #calculate mean and SD for selected variable
## Error in select(., base_IAT_gen): object 'cleandata' not found
print(BaselineGender)
## Error in print(BaselineGender): object 'BaselineGender' not found
Calculating prenap race bias
PrenapRace <- cleandata %>% #creating new data "PrenapRace" from "cleandata"
select(pre_IAT_race) %>% #select variable "pre_IAT_race" from "cleandata"
summarise(PrenapRaceMean = mean(pre_IAT_race), PrenapRaceSD = sd(pre_IAT_race)) #calculate mean and SD for selected variable
## Error in select(., pre_IAT_race): object 'cleandata' not found
print(PrenapRace)
## Error in print(PrenapRace): object 'PrenapRace' not found
Calculating prenap gender bias
PrenapGender <- cleandata %>% #creating new data "PrenapGender" from "cleandata"
select(pre_IAT_gen) %>% #select variable "PrenapGender" from "cleandata"
summarise(Prenapgendermean = mean(pre_IAT_gen), PrenapRaceSD = sd(pre_IAT_gen)) #calculate mean and SD for selected variable
## Error in select(., pre_IAT_gen): object 'cleandata' not found
print(PrenapGender)
## Error in print(PrenapGender): object 'PrenapGender' not found
Credits to Julia for finding this hack
implicitbiaslevels <- cleandata %>%
select(base_IAT_race, base_IAT_gen, pre_IAT_race, pre_IAT_gen) #Selecting these variables from the "cleandata" dataset to create new data "implicitbiaslevels"
## Error in select(., base_IAT_race, base_IAT_gen, pre_IAT_race, pre_IAT_gen): object 'cleandata' not found
#now I'm going to calculate the mean and sd of all of these variables:
implicitbiaslevels <- implicitbiaslevels %>%
summarise(across(contains("IAT"), list(mean = mean, sd = sd))) #captures and calculates the means and SDs of each variable that contains "IAT" within its variable name. Lists the calculated means and sds for each variable under the label "mean" and "sd".
## Error in summarise(., across(contains("IAT"), list(mean = mean, sd = sd))): object 'implicitbiaslevels' not found
#now the data "implicitbiaslevels" has the means and SDs for each of the variables previously selected, instead of having to separately code for each mean and SD needed and then having to create a new dataframe.
Load needed packages. We are using gt() package again because all the research we have done has indicated that this package is the simplest to format and use.
library(gt)
library(glue)
##
## Attaching package: 'glue'
## The following object is masked from 'package:dplyr':
##
## collapse
#Tried to recreate Table 2 using simplified code and dataset "implicitbiaslevels". We were hoping that this simplified code could be used to quickly and simply recreate the table.
implicitbiaslevels %>%
gt() %>%
tab_header(
title = "Table 2: Race and Gender Implicit Bias Levels") %>% #title
tab_source_note("Implicit bias values are the average D600 score for each timepoint") %>% #creates source note in the table footer
fmt_number(columns = vars(mean1, mean2, SD1, SD2), decimals = 2) %>% #formats values to 2 decimal places
#vars() function used to select variables to input into the columns
tab_spanner(
label = "Baseline", #Spanner Column label
columns = c(mean1, SD1) #selects variables "mean1" and "SD1" for the 2 columns underneath the spanner column label "Baseline"
) %>%
tab_spanner(
label = "Prenap", #Spanner Column label
columns = c(mean2, SD2) #selects variables "mean2" and "SD2" for the 2 columns underneath the spanner column label "Prenap"
) %>%
tab_row_group(
label = "Race", #label for row group
rows = 1 #1 row for this row group
) %>%
tab_row_group(
label = "Gender", #label for row group
rows = 2 #2 rows for this row group
)
## Error in dplyr::group_vars(data): object 'implicitbiaslevels' not found
print(implicitbiaslevels)
## Error in print(implicitbiaslevels): object 'implicitbiaslevels' not found
#Works but does not let the mean be inputted alone - needs to be identified as separate columns, stub labels also don't sit flush but look more like a subheader
Manually inputting the data:
#Since using the simplified dataset "implicitbiaslevels" didn't work, I tried inserting/formatting the same values differently by creating a new dataframe with different labels.
#values are taken from the "implicitbiaslevels" data.
table2 <- tibble( #3 columns
Variables = c("base_IAT_race", "base_IAT_gen", "pre_IAT_race", "pre_IAT_gen"), #new variable names
Mean = c(0.619, 0.494, 0.202, 0.311),
SD = c(0.442, 0.362, 0.563, 0.375)
)
print(table2)
## # A tibble: 4 x 3
## Variables Mean SD
## <chr> <dbl> <dbl>
## 1 base_IAT_race 0.619 0.442
## 2 base_IAT_gen 0.494 0.362
## 3 pre_IAT_race 0.202 0.563
## 4 pre_IAT_gen 0.311 0.375
Recreate Table 2 using manually-inputed data "table2"
table2 %>%
gt( #use gt() package to create table
rowname_col = "Race", "Gender" #gave the 2 row names "Race" and "Gender"
) %>%
tab_header(
title = "Table 2: Race and Gender Implicit Bias Levels") %>% #title for the table
tab_source_note("Implicit bias values are the average D600 score for each timepoint") %>% #creates source note in the table footer
fmt_number(columns = vars(Mean, SD, Mean, SD), decimals = 2) %>% #formats values to 2 decimal places
#vars() function used to select variables to input into the columns
tab_spanner(
label = "Baseline", #Spanner column label
columns = c(base_IAT_race, base_IAT_gen) #selects variables "base_IAT_race" and "base_IAT_gen" for the 2 columns underneath the spanner column label "Baseline"
) %>%
tab_spanner(
label = "Prenap", #Spanner column label
columns = c(pre_IAT_race, pre_IAT_gen) #selects variables "pre_IAT_race" and "pre_IAT_gen" for the 2 columns underneath the spanner column label "Prenap"
)
## Warning: `columns = vars(...)` has been deprecated in gt 0.3.0:
## * please use `columns = c(...)` instead
## Warning: `columns = vars(...)` has been deprecated in gt 0.3.0:
## * please use `columns = c(...)` instead
## Error: Can't subset columns that don't exist.
## x Column `base_IAT_race` doesn't exist.
#Keeps giving this error: "Error: Can't subset columns that don't exist. x Column `Mean` doesn't exist."
Create new dataframe "table_2"
#Tried formatting values into a new dataframe to more closely match the table that we are trying to reproduce. Values are taken from the same "implicitbiaslevels" data as before.
table_2 <- tibble( #4 different columns
mean1 = c(0.6186929, 0.4943818),
mean2 = c(0.2023364, 0.3109984),
SD1 = c(0.4423884, 0.36228),
SD2 = c(0.5633004, 0.3748071)
)
print(table_2)
## # A tibble: 2 x 4
## mean1 mean2 SD1 SD2
## <dbl> <dbl> <dbl> <dbl>
## 1 0.619 0.202 0.442 0.563
## 2 0.494 0.311 0.362 0.375
Recreate Table 2 using "table_2" dataframe
table_2 %>%
gt() %>% #use gt() package to create table
tab_header( #format table header
title = "Table 2: Race and Gender Implicit Bias Levels") %>% #title of table
tab_source_note("Implicit bias values are the average D600 score for each timepoint") %>% #creates source note in the table footer
fmt_number(columns = vars(mean1, mean2, SD1, SD2), decimals = 2) %>% #formats values to 2 decimal places
#vars() function used to select variables to input into the columns
tab_spanner(
label = "Baseline", #Spanner column label
columns = c(mean1, SD1) #selects variables "mean1" and "SD1" for the 2 columns underneath the spanner column label "Baseline"
) %>%
tab_spanner(
label = "Prenap", #Spanner column label
columns = c(mean2, SD2) #selects variables "mean2" and "SD2" for the 2 columns underneath the spanner column label "Prenap"
) %>%
tab_row_group(
label = "Race", #Row group label
rows = 1 #1 rows underneath this row group
) %>%
tab_row_group(
label = "Gender", #Row group label
rows = 2 #2 rows underneath this row group
)
## Warning: `columns = vars(...)` has been deprecated in gt 0.3.0:
## * please use `columns = c(...)` instead
## Warning: `columns = vars(...)` has been deprecated in gt 0.3.0:
## * please use `columns = c(...)` instead
| Table 2: Race and Gender Implicit Bias Levels | |||
|---|---|---|---|
| Baseline | Prenap | ||
| mean1 | SD1 | mean2 | SD2 |
| Gender | |||
| 0.49 | 0.36 | 0.31 | 0.37 |
| Race | |||
| 0.62 | 0.44 | 0.20 | 0.56 |
| Implicit bias values are the average D600 score for each timepoint | |||
print(table_2)
## # A tibble: 2 x 4
## mean1 mean2 SD1 SD2
## <dbl> <dbl> <dbl> <dbl>
## 1 0.619 0.202 0.442 0.563
## 2 0.494 0.311 0.362 0.375
#Format is now finally showing as is seen in original paper
#However, outputted column names as "mean1", "SD1", "mean2", "SD2", which is not identical to what is seen in the paper
#Need to figure out how to change these tab names to "Mean" and "SD"
#Used same code as before but getting rid of tab_row_group() function (that was used to label the group row names) and
#Also, instead using cols_label() function to label the group columns
table_2 %>%
gt() %>% #use gt() package
tab_header(
title = "Table 2: Race and Gender Implicit Bias Levels") %>% #table title
tab_source_note("Implicit bias values are the average D600 score for each timepoint") %>% #creates source note in the table footer
fmt_number(columns = vars(mean1, mean2, SD1, SD2), decimals = 2) %>% #formats values to 2 decimal places
#vars() function used to select variables to input into the columns
tab_spanner(
label = "Baseline",
columns = c(mean1, SD1)
) %>%
tab_spanner(
label = "Prenap",
columns = c(mean2, SD2)
) %>%
cols_label(mean1 = "mean", mean2 = "mean", SD1 = "SD", SD2 = "SD", label = " ") #changed column labels
## Warning: `columns = vars(...)` has been deprecated in gt 0.3.0:
## * please use `columns = c(...)` instead
## Warning: `columns = vars(...)` has been deprecated in gt 0.3.0:
## * please use `columns = c(...)` instead
## Error in cols_label(., mean1 = "mean", mean2 = "mean", SD1 = "SD", SD2 = "SD", : All column names provided must exist in the input `data` table.
#Kept giving error: "Error in cols_label(., mean1 = "mean", mean2 = "mean", SD1 = "SD", SD2 = "SD", : All column names provided must exist in the input `data` table."
Thus tried creating new dataframe with labels
table2_label <- tibble(
label = c("Race", "Gender"),
mean1 = c(0.6186929, 0.4943818),
mean2 = c(0.2023364, 0.3109984),
SD1 = c(0.4423884, 0.36228),
SD2 = c(0.5633004, 0.3748071)
)
print(table2_label)
## # A tibble: 2 x 5
## label mean1 mean2 SD1 SD2
## <chr> <dbl> <dbl> <dbl> <dbl>
## 1 Race 0.619 0.202 0.442 0.563
## 2 Gender 0.494 0.311 0.362 0.375
Now, tried using the same code as previously but with new dataframe:
table2_label %>%
gt() %>%
tab_header(
title = "Table 2. Race and gender implicit bias levels") %>%
tab_source_note("Implicit bias values are the average D600 score for each timepoint") %>% #creates source note in the table footer
fmt_number(columns = vars(mean1, mean2, SD1, SD2), decimals = 2) %>% #formats values to 2 decimal places
#vars() function used to select variables to input into the columns
tab_spanner(
label = "Baseline", #Spanner column label
columns = c(mean1, SD1) #selects variables "mean1" and "SD1" for the 2 columns underneath the spanner column label "Baseline"
) %>%
tab_spanner(
label = "Prenap", #Spanner column label
columns = c(mean2, SD2) #selects variables "mean2" and "SD2" for the 2 columns underneath the spanner column label "Prenap"
) %>%
cols_label(mean1 = "Mean", mean2 = "Mean", SD1 = "SD", SD2 = "SD", label = " ") #changes the column labels for each one
## Warning: `columns = vars(...)` has been deprecated in gt 0.3.0:
## * please use `columns = c(...)` instead
## Warning: `columns = vars(...)` has been deprecated in gt 0.3.0:
## * please use `columns = c(...)` instead
| Table 2. Race and gender implicit bias levels | ||||
|---|---|---|---|---|
| Baseline | Prenap | |||
| Mean | SD | Mean | SD | |
| Race | 0.62 | 0.44 | 0.20 | 0.56 |
| Gender | 0.49 | 0.36 | 0.31 | 0.37 |
| Implicit bias values are the average D600 score for each timepoint | ||||
#originalvariablename = "New column name"
print(table2_label)
## # A tibble: 2 x 5
## label mean1 mean2 SD1 SD2
## <chr> <dbl> <dbl> <dbl> <dbl>
## 1 Race 0.619 0.202 0.442 0.563
## 2 Gender 0.494 0.311 0.362 0.375
#Everything looks perfect!