---
# System prepended metadata

title: figure 3

---

# figure 3

data3 <- data %>% 
  rename(immunity = q8haveimmunity,
         outings = Going_out_total,
         noworry = q9worry,
         rself = q10arisk,
         rothers = q10brisk,
         think_covid = Ever_covid) %>% 
  select(immunity, 
         outings,
         noworry,
         rself,
         rothers,
         think_covid) %>% 
  mutate(think_covid = case_when(think_covid == 1 ~ "Have COVID-19",
                                 think_covid == 0 ~ "Have Not Had COVID-19")) 

#Creating separate data sets to calculate mean and standard deviation

#Immunity
d3immune <- data3 %>% 
  select(immunity, think_covid) %>% 
  group_by(think_covid) %>% 
  summarise(mean(immunity),
            sd(immunity)) %>% 
  rename(mean = 'mean(immunity)',
         sd = 'sd(immunity)')

d3immune <- data.frame(lapply(d3immune,
                               function(x) if(is.numeric(x)) round(x,2) else x))

#d3immune$desc <- paste(d3immune$mean, d3immune$sd, sep=",") 

d3immune$desc <- paste("M = ",d3immune$mean, sep="") %>% 
  paste(", SD = ",d3immune$sd, sep="") 

d3immune <- subset(d3immune, select = -c(mean,sd))

#Outings

d3outings <- data3 %>% 
  select(outings, think_covid) %>% 
  group_by(think_covid) %>% 
  summarise(mean(outings),
            sd(outings)) %>% 
  rename(mean = 'mean(outings)',
         sd = 'sd(outings)')

d3outings <- data.frame(lapply(d3outings,
                               function(x) if(is.numeric(x)) round(x,2) else x))

d3outings$desc <- paste("M = ",d3outings$mean, sep="") %>% 
  paste(", SD = ",d3outings$sd, sep="") 

d3outings <- subset(d3outings, select = -c(mean,sd))

#No worry

d3noworry <- data3 %>% 
  select(noworry, think_covid) %>% 
  group_by(think_covid) %>% 
  summarise(mean(noworry),
            sd(noworry)) %>% 
  rename(mean = 'mean(noworry)',
         sd = 'sd(noworry)')

d3noworry <- data.frame(lapply(d3noworry,
                               function(x) if(is.numeric(x)) round(x,2) else x))

d3noworry$desc <- paste("M = ",d3noworry$mean, sep="") %>% 
  paste(", SD = ",d3noworry$sd, sep="") 

d3noworry <- subset(d3noworry, select = -c(mean,sd))

#Risk to self

d3rself <- data3 %>% 
  select(rself, think_covid) %>% 
  group_by(think_covid) %>% 
  summarise(mean(rself),
            sd(rself)) %>% 
  rename(mean = 'mean(rself)',
         sd = 'sd(rself)')

d3rself <- data.frame(lapply(d3rself,
                               function(x) if(is.numeric(x)) round(x,2) else x))

d3rself$desc <- paste("M = ",d3rself$mean, sep="") %>% 
  paste(", SD = ",d3rself$sd, sep="") 
  

d3rself <- subset(d3rself, select = -c(mean,sd))

#Risk to others

d3rothers <- data3 %>% 
  select(rothers, think_covid) %>% 
  group_by(think_covid) %>% 
  summarise(mean(rothers),
            sd(rothers)) %>% 
  rename(mean = 'mean(rothers)',
         sd = 'sd(rothers)') 

d3rothers <- data.frame(lapply(d3rothers,
                                function(x) if(is.numeric(x)) round(x,2) else x))

d3rothers$desc <- paste("M = ",d3rothers$mean, sep="") %>% 
  paste(", SD = ",d3rothers$sd, sep="") 

d3rothers <- subset(d3rothers, select = -c(mean,sd))

#Switching the table around to have Think have not as column labels

switchI <- as.data.frame(t(as.matrix(d3immune)))
switchO <- as.data.frame(t(as.matrix(d3outings)))
switchW <- as.data.frame(t(as.matrix(d3noworry)))
switchS <- as.data.frame(t(as.matrix(d3rself)))
switchR <- as.data.frame(t(as.matrix(d3rothers)))

#turning into a table
fulllist3 <- list(switchI, switchO, switchW, switchS, switchR)
fulltable3 <- rbindlist(fulllist3, use.name = FALSE)

#cleaning up fulltable3
fulltable3 <- fulltable3[-c(1, 3, 5, 7, 9)] %>% 
  select(2, 1) 


#roundfulltable3 <- function(fulltable3, digits=3)
#numeric_columns <- sapply(fulltable3, class) == 'numeric'
#fulltable3<- round(fulltable3[numeric_columns], digits=3)

#making new columns for levels and p characteristics

level3 <- c("1 = strongly disagree to 5 = strongly agree",
            "Range = 0 to 42",
            "1 = not at all worried to 5 = extremely worried",
            "1 = no risk at all to 4 = major risk",
            "1 = no risk at all to 4 = major risk")

pchar3 <- c("I think I have some immunity to COVID-19",
            "Total out-of-home activity in the last seven days",
            "Worry about COVID-19",
            "Perceived risk of COVID-19 to oneself",
            "Perceived risk of COVID-19 to people in the UK")
  
#merging new and table using cbind

fulltable3 <- cbind(pchar3, level3, fulltable3) 

gtfulltable3 <- gt(fulltable3) %>% 
  tab_spanner(
  label = "Had COVID-19",
  columns = c(3:4)
) %>%
  cols_label(
    pchar3 = "Participant characteristics"
  ) %>% 
  cols_label(
    "level3" = "Level"
  ) %>% 
  cols_label(
    "2" = "Think have not had COVID-19 n = 4656"
  ) %>% 
  cols_label(
    "1" = "Think have had COVID-19 n = 1493"
  ) %>% 
  tab_style(cell_text(weight = "bold"), 
            locations = cells_column_labels(columns = everything())) %>% 
  tab_style(cell_text(weight = "bold"),
            locations = cells_column_spanners())