Week 6: Learning Log

My Coding Goals this Week

My goals this week were to help fix up table 2 for our report. Some of our group members had made a start on table 2 and created the tibble required, but we have to rename the participant characteristics, add a title and add the subheading under “Had COVID-19.”

I was skeptical that I would be able to help, but I knew I would be likely to have more success in adding the title and subheading. I have been busy doing an assessment for my MGMT course so I will try to get as much of my part done for the presentation as I can as well.

Downloading Everything

# setting the CRAN mirror
options(repos = c(CRAN = "http://cran.rstudio.com"))

install.packages("rmdformats")
## 
## The downloaded binary packages are in
##  /var/folders/cw/l9bfyrms3md0tbkr1866zbl80000gn/T//Rtmp05OyHq/downloaded_packages
install.packages("gt")
## 
## The downloaded binary packages are in
##  /var/folders/cw/l9bfyrms3md0tbkr1866zbl80000gn/T//Rtmp05OyHq/downloaded_packages
install.packages("haven")
## 
## The downloaded binary packages are in
##  /var/folders/cw/l9bfyrms3md0tbkr1866zbl80000gn/T//Rtmp05OyHq/downloaded_packages
library(rmdformats)
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.0 ──
## ✓ ggplot2 3.3.3     ✓ purrr   0.3.4
## ✓ tibble  3.1.0     ✓ dplyr   1.0.5
## ✓ 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(knitr)
library(dplyr)
library(gt)
library(haven)

Challenges and Successes

This is the tibble I am working with (I can’t take credit for this one!).

I changed this: “Think have not had COVID-19 n = 4656”, “Think have had COVID-19 n = 1493,” in order to match the original table we are reproducing. This will be our subheading under “Had COVID-19.”

COVID <- read_sav(file ="coviddata.sav")

# the tibble
 MSD <- COVID %>% 
   group_by(Ever_covid) %>% 
   summarise(across(c(q8haveimmunity,Going_out_total,q9worry,q10arisk,q10brisk),
                    list(mean = mean, standard_deviation = sd)))
print(MSD)
## # A tibble: 2 x 11
##   Ever_covid q8haveimmunity_… q8haveimmunity_… Going_out_total… Going_out_total…
##    <dbl+lbl>            <dbl>            <dbl>            <dbl>            <dbl>
## 1 0 [Think …             2.38             1.01             6.69             5.63
## 2 1 [Think …             3.33             1.00             9.35             7.69
## # … with 6 more variables: q9worry_mean <dbl>,
## #   q9worry_standard_deviation <dbl>, q10arisk_mean <dbl>,
## #   q10arisk_standard_deviation <dbl>, q10brisk_mean <dbl>,
## #   q10brisk_standard_deviation <dbl>
table2mean <- MSD %>% 
  select(ends_with("mean"), Ever_covid) %>% 
  pivot_longer(ends_with("mean") ,  names_to = "Mean_vars", values_to = "Mean_values")

table2SD <- MSD %>% 
  select(ends_with("deviation"), Ever_covid) %>% 
  pivot_longer(ends_with("deviation") ,  names_to = "SD_vars", values_to = "SD_values")
  
  table2 <- bind_cols(table2SD,table2mean) %>% 
  mutate(real = paste0("M = ", Mean_values,",", " SD = ", SD_values)) %>% 
  select(Ever_covid...1, Mean_vars, real) %>% 
  mutate(Level = case_when(startsWith(Mean_vars,"q8") ~ "1 = Strongly disagree to, 5 = Strongly agree", startsWith(Mean_vars,"q9") ~"1 = not worried at all to,  5 = extremely worried", 
                           startsWith(Mean_vars, "Going") ~"Range = 0 to 42", TRUE ~"1 = not risk at all to , 4 = major risk ")) %>% 
  rename("Participant characteristics" = "Mean_vars") 
## New names:
## * Ever_covid -> Ever_covid...1
## * Ever_covid -> Ever_covid...4
  table2$Ever_covid...1 <- as.factor(table2$Ever_covid...1)
  table2$`Participant characteristics` <- as.factor(table2$`Participant characteristics`)
  table2$Ever_covid...1 <- factor(table2$Ever_covid...1, labels = c("Think have not had COVID-19 n = 4656", "Think have had COVID-19 n = 1493"))
  
  table2 <- table2 %>% 
    pivot_wider(id_cols = -real, names_from = Ever_covid...1, values_from = real)
  
table2
## # A tibble: 5 x 4
##   `Participant chara… Level          `Think have not had … `Think have had COVI…
##   <fct>               <chr>          <chr>                 <chr>                
## 1 q8haveimmunity_mean "1 = Strongly… M = 2.38273195876289… M = 3.33288680509042…
## 2 Going_out_total_me… "Range = 0 to… M = 6.68578178694158… M = 9.35097119892833…
## 3 q9worry_mean        "1 = not worr… M = 3.59342783505155… M = 3.37508372404555…
## 4 q10arisk_mean       "1 = not risk… M = 2.80713058419244… M = 2.80910917615539…
## 5 q10brisk_mean       "1 = not risk… M = 3.38767182130584… M = 3.30207635632954…
# for the gt table
gt_tbl <- gt(data = table2)  
gt_tbl
Participant characteristics Level Think have not had COVID-19 n = 4656 Think have had COVID-19 n = 1493
q8haveimmunity_mean 1 = Strongly disagree to, 5 = Strongly agree M = 2.38273195876289, SD = 1.01333447393515 M = 3.33288680509042, SD = 1.00453237552535
Going_out_total_mean Range = 0 to 42 M = 6.68578178694158, SD = 5.63156858399883 M = 9.35097119892833, SD = 7.68987056528471
q9worry_mean 1 = not worried at all to, 5 = extremely worried M = 3.59342783505155, SD = 1.00859452696213 M = 3.37508372404555, SD = 1.11951765589453
q10arisk_mean 1 = not risk at all to , 4 = major risk M = 2.80713058419244, SD = 0.761422613443492 M = 2.80910917615539, SD = 0.755052736473036
q10brisk_mean 1 = not risk at all to , 4 = major risk M = 3.38767182130584, SD = 0.669618799574744 M = 3.30207635632954, SD = 0.701761267243595
# adding a title and subheading
gt_tbl <-
  gt_tbl %>% 
  tab_header(
    title = "Table 2. Associations between thinking you have had COVID-19 and perceived immunity to COVID-19; worry about COVID-19; perceived risk of to COVID-19 (to oneself and people in the UK); and total out-of-home activities in the last seven days (continuous outcomes). "
  ) %>% 
  tab_spanner(
    label = "Had COVID 19",
    columns = vars("Think have not had COVID-19 n = 4656", "Think have had COVID-19 n = 1493")
  )

gt_tbl
Table 2. Associations between thinking you have had COVID-19 and perceived immunity to COVID-19; worry about COVID-19; perceived risk of to COVID-19 (to oneself and people in the UK); and total out-of-home activities in the last seven days (continuous outcomes).
Participant characteristics Level Had COVID 19
Think have not had COVID-19 n = 4656 Think have had COVID-19 n = 1493
q8haveimmunity_mean 1 = Strongly disagree to, 5 = Strongly agree M = 2.38273195876289, SD = 1.01333447393515 M = 3.33288680509042, SD = 1.00453237552535
Going_out_total_mean Range = 0 to 42 M = 6.68578178694158, SD = 5.63156858399883 M = 9.35097119892833, SD = 7.68987056528471
q9worry_mean 1 = not worried at all to, 5 = extremely worried M = 3.59342783505155, SD = 1.00859452696213 M = 3.37508372404555, SD = 1.11951765589453
q10arisk_mean 1 = not risk at all to , 4 = major risk M = 2.80713058419244, SD = 0.761422613443492 M = 2.80910917615539, SD = 0.755052736473036
q10brisk_mean 1 = not risk at all to , 4 = major risk M = 3.38767182130584, SD = 0.669618799574744 M = 3.30207635632954, SD = 0.701761267243595

I then used the gt package to create the title and subheading, but I couldn’t figure out how to change the names of the participant characteristics. I have put my failures here. I honestly just looked up how to rename factors online so I’m not really sure how this code works and why it’s not working. That’ll be a challenge for next week!

First attempt (fail)

sizes <- factor(c(“q8haveimmunity_mean”, “Going_out_total_mean”, “q9worry_mean”, “q10arisk_mean”, “q10brisk_mean”))

fct_recode(sizes, I think I have some immunity to COVID-19 = “q8haveimmunity_mean”, Total out-of-home activity in the last seven days = “Going_out_total_mean”, Worry about COVID-19 = “q9worry_mean”, Perceived risk of COVID-10 to onself = “q10arisk_mean”, Perceived risk of COVID-19 to people in the UK = “q10brisk_mean”)

Second attempt (fail also)

x <- factor(c(“q8haveimmunity_mean”, “Going_out_total_mean”, “q9worry_mean”, “q10arisk_mean”, "q10brisk_mean))

levels(x) <- list(I think I have some immunity to COVID-19=“q8haveimmunity_mean”, Total out-of-home activity in the last seven days = “Going_out_total_mean”)

Next Steps…

My next steps are to continue helping with the table and then start answering the questions for our presentation in Week 8. <- this is actually last week’s goal, which I didn’t get around to doing (sadly). I feel a bit disappointed as this week was flexi week, however I made a lot of progress on my mgmt assignment, and tried to sneak in some downtime as well.

  • I’m going to familiarise myself with the gt package
  • Do my bit for our Week 8 presentation
  • Help my group figure out the rest of Table 2

Thank you for reading my learning log :)