R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

Mission 1

# List of packages
packages <- c("tidyverse", "modelsummary", "forcats", "RColorBrewer", 
              "fst", "viridis", "knitr", "rmarkdown", "ggridges", "viridis", "questionr", "flextable", "infer") # add 

# Install packages if they aren't installed already
new_packages <- packages[!(packages %in% installed.packages()[,"Package"])]
if(length(new_packages)) install.packages(new_packages)

# Load the packages
lapply(packages, library, character.only = TRUE)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.3     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ ggplot2   3.4.3     ✔ tibble    3.2.1
## ✔ lubridate 1.9.2     ✔ tidyr     1.3.0
## ✔ 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
## Loading required package: viridisLite
## 
## 
## Attaching package: 'flextable'
## 
## 
## The following object is masked from 'package:purrr':
## 
##     compose
## [[1]]
##  [1] "lubridate" "forcats"   "stringr"   "dplyr"     "purrr"     "readr"    
##  [7] "tidyr"     "tibble"    "ggplot2"   "tidyverse" "stats"     "graphics" 
## [13] "grDevices" "utils"     "datasets"  "methods"   "base"     
## 
## [[2]]
##  [1] "modelsummary" "lubridate"    "forcats"      "stringr"      "dplyr"       
##  [6] "purrr"        "readr"        "tidyr"        "tibble"       "ggplot2"     
## [11] "tidyverse"    "stats"        "graphics"     "grDevices"    "utils"       
## [16] "datasets"     "methods"      "base"        
## 
## [[3]]
##  [1] "modelsummary" "lubridate"    "forcats"      "stringr"      "dplyr"       
##  [6] "purrr"        "readr"        "tidyr"        "tibble"       "ggplot2"     
## [11] "tidyverse"    "stats"        "graphics"     "grDevices"    "utils"       
## [16] "datasets"     "methods"      "base"        
## 
## [[4]]
##  [1] "RColorBrewer" "modelsummary" "lubridate"    "forcats"      "stringr"     
##  [6] "dplyr"        "purrr"        "readr"        "tidyr"        "tibble"      
## [11] "ggplot2"      "tidyverse"    "stats"        "graphics"     "grDevices"   
## [16] "utils"        "datasets"     "methods"      "base"        
## 
## [[5]]
##  [1] "fst"          "RColorBrewer" "modelsummary" "lubridate"    "forcats"     
##  [6] "stringr"      "dplyr"        "purrr"        "readr"        "tidyr"       
## [11] "tibble"       "ggplot2"      "tidyverse"    "stats"        "graphics"    
## [16] "grDevices"    "utils"        "datasets"     "methods"      "base"        
## 
## [[6]]
##  [1] "viridis"      "viridisLite"  "fst"          "RColorBrewer" "modelsummary"
##  [6] "lubridate"    "forcats"      "stringr"      "dplyr"        "purrr"       
## [11] "readr"        "tidyr"        "tibble"       "ggplot2"      "tidyverse"   
## [16] "stats"        "graphics"     "grDevices"    "utils"        "datasets"    
## [21] "methods"      "base"        
## 
## [[7]]
##  [1] "knitr"        "viridis"      "viridisLite"  "fst"          "RColorBrewer"
##  [6] "modelsummary" "lubridate"    "forcats"      "stringr"      "dplyr"       
## [11] "purrr"        "readr"        "tidyr"        "tibble"       "ggplot2"     
## [16] "tidyverse"    "stats"        "graphics"     "grDevices"    "utils"       
## [21] "datasets"     "methods"      "base"        
## 
## [[8]]
##  [1] "rmarkdown"    "knitr"        "viridis"      "viridisLite"  "fst"         
##  [6] "RColorBrewer" "modelsummary" "lubridate"    "forcats"      "stringr"     
## [11] "dplyr"        "purrr"        "readr"        "tidyr"        "tibble"      
## [16] "ggplot2"      "tidyverse"    "stats"        "graphics"     "grDevices"   
## [21] "utils"        "datasets"     "methods"      "base"        
## 
## [[9]]
##  [1] "ggridges"     "rmarkdown"    "knitr"        "viridis"      "viridisLite" 
##  [6] "fst"          "RColorBrewer" "modelsummary" "lubridate"    "forcats"     
## [11] "stringr"      "dplyr"        "purrr"        "readr"        "tidyr"       
## [16] "tibble"       "ggplot2"      "tidyverse"    "stats"        "graphics"    
## [21] "grDevices"    "utils"        "datasets"     "methods"      "base"        
## 
## [[10]]
##  [1] "ggridges"     "rmarkdown"    "knitr"        "viridis"      "viridisLite" 
##  [6] "fst"          "RColorBrewer" "modelsummary" "lubridate"    "forcats"     
## [11] "stringr"      "dplyr"        "purrr"        "readr"        "tidyr"       
## [16] "tibble"       "ggplot2"      "tidyverse"    "stats"        "graphics"    
## [21] "grDevices"    "utils"        "datasets"     "methods"      "base"        
## 
## [[11]]
##  [1] "questionr"    "ggridges"     "rmarkdown"    "knitr"        "viridis"     
##  [6] "viridisLite"  "fst"          "RColorBrewer" "modelsummary" "lubridate"   
## [11] "forcats"      "stringr"      "dplyr"        "purrr"        "readr"       
## [16] "tidyr"        "tibble"       "ggplot2"      "tidyverse"    "stats"       
## [21] "graphics"     "grDevices"    "utils"        "datasets"     "methods"     
## [26] "base"        
## 
## [[12]]
##  [1] "flextable"    "questionr"    "ggridges"     "rmarkdown"    "knitr"       
##  [6] "viridis"      "viridisLite"  "fst"          "RColorBrewer" "modelsummary"
## [11] "lubridate"    "forcats"      "stringr"      "dplyr"        "purrr"       
## [16] "readr"        "tidyr"        "tibble"       "ggplot2"      "tidyverse"   
## [21] "stats"        "graphics"     "grDevices"    "utils"        "datasets"    
## [26] "methods"      "base"        
## 
## [[13]]
##  [1] "infer"        "flextable"    "questionr"    "ggridges"     "rmarkdown"   
##  [6] "knitr"        "viridis"      "viridisLite"  "fst"          "RColorBrewer"
## [11] "modelsummary" "lubridate"    "forcats"      "stringr"      "dplyr"       
## [16] "purrr"        "readr"        "tidyr"        "tibble"       "ggplot2"     
## [21] "tidyverse"    "stats"        "graphics"     "grDevices"    "utils"       
## [26] "datasets"     "methods"      "base"
# read data
setwd("~/Desktop/SOC_202_YAY/")

getwd()  
## [1] "/Users/apple/Desktop/SOC_202_YAY"
ess <- read_fst("All-ESS-Data.fst")

Mission 2

uk_data <- ess %>% 
  filter(cntry == "GB") 
write_fst(uk_data, "/Users/apple/Desktop/SOC_202_YAY/uk_data.fst") 

Mission 3

rm(list=ls()); gc()
##           used (Mb) gc trigger   (Mb) limit (Mb)   max used    (Mb)
## Ncells 1253155 67.0    2116280  113.1         NA    2116280   113.1
## Vcells 2112785 16.2 1256969650 9590.0      16384 1361143084 10384.7
df <- read_fst("uk_data.fst")

Mission 4

df$year <- NA
replacements <- c(2002, 2004, 2006, 2008, 2010, 2012, 2014, 2016, 2018, 2020)
for(i in 1:10){
  df$year[df$essround == i] <- replacements[i]
}

uk_data <- df

# make sure you know your current folder path. you can do getwd() to check
uk_data_table_subset <- uk_data %>%
  mutate(
    happy = ifelse(happy == 2, 0, ifelse(happy %in% c(7, 8, 9), NA, happy)), 
    imprich = ifelse(imprich %in% c(77, 88, 99), NA, imprich), 
    trstplt = ifelse(trstplt %in% c(77, 88, 99), NA, trstplt) 
  )

summary_table <- datasummary_skim(uk_data_table_subset %>% select(happy, imprich, trstplt), output = "flextable", title= "uk")
## Warning: The histogram argument is only supported for (a) output types "default",
##   "html", "kableExtra", or "gt"; (b) writing to file paths with extensions
##   ".html", ".jpg", or ".png"; and (c) Rmarkdown, knitr or Quarto documents
##   compiled to PDF (via kableExtra)  or HTML (via kableExtra or gt). Use
##   `histogram=FALSE` to silence this warning.
summary_table
uk

Unique (#)

Missing (%)

Mean

SD

Min

Median

Max

happy

10

65

6.8

6.1

0.0

6.0

88.0

imprich

10

1

4.4

1.3

1.0

5.0

9.0

trstplt

12

1

3.5

2.3

0.0

4.0

10.0

uk_data_v2 <- uk_data_table_subset %>%
  rename(
    `How happy are you` = happy,
    `Important to be rich, have money and expensive things` = imprich,
    `Trust in Politicians` = trstplt
  )

summary_table_v2 <- datasummary_skim(uk_data_v2 %>% select(`How happy are you`,`Important to be rich, have money and expensive things`, `Trust in Politicians`), output = "flextable")
## Warning: The histogram argument is only supported for (a) output types "default",
##   "html", "kableExtra", or "gt"; (b) writing to file paths with extensions
##   ".html", ".jpg", or ".png"; and (c) Rmarkdown, knitr or Quarto documents
##   compiled to PDF (via kableExtra)  or HTML (via kableExtra or gt). Use
##   `histogram=FALSE` to silence this warning.
summary_table_v2

Unique (#)

Missing (%)

Mean

SD

Min

Median

Max

How happy are you

10

65

6.8

6.1

0.0

6.0

88.0

Important to be rich, have money and expensive things

10

1

4.4

1.3

1.0

5.0

9.0

Trust in Politicians

12

1

3.5

2.3

0.0

4.0

10.0

flextable::save_as_docx(summary_table_v2, path = "summary_table_v2.docx", # change name to whatever you would like
                       width = 7.0, height = 7.0) 

Mission 5

df$year <- NA
replacements <- c(2002, 2004, 2006, 2008, 2010, 2012, 2014, 2016, 2018, 2020)
for(i in 1:10){
  df$year[df$essround == i] <- replacements[i]
}

uk_data <- df

uk_data <- uk_data %>%
  mutate(religion = case_when(
    rlgblg == 2 ~ "No",
    rlgblg == 1 ~ "Yes",
    rlgblg %in% c(7, 8, 9) ~ NA_character_,
    TRUE ~ as.character(rlgblg)
  ))

table(uk_data$religion)
## 
##    No   Yes 
## 10923 10020
table(uk_data$rlgblg)
## 
##     1     2     7     8 
## 10020 10923     9    27
uk_data <- uk_data %>%
  mutate(geo = recode(as.character(domicil), 
                      '1' = "Urban", 
                      '2' = "Peri-Urban", # or set to Urban | Regardless decision needs to be justified
                      '3' = "Rural", 
                      '4' = "Rural", 
                      '5' = "Rural",
                      '7' = NA_character_,
                      '8' = NA_character_,
                      '9' = NA_character_))

table(uk_data$geo)
## 
## Peri-Urban      Rural      Urban 
##       4615      14550       1761
table(uk_data$domicil)
## 
##    1    2    3    4    5    7    8    9 
## 1761 4615 9559 4250  741    6   37   10
summary_table <- datasummary_skim(uk_data_table_subset %>% select(rlgblg, domicil), output = "flextable", title ="Socio-demographic variables")
## Warning: The histogram argument is only supported for (a) output types "default",
##   "html", "kableExtra", or "gt"; (b) writing to file paths with extensions
##   ".html", ".jpg", or ".png"; and (c) Rmarkdown, knitr or Quarto documents
##   compiled to PDF (via kableExtra)  or HTML (via kableExtra or gt). Use
##   `histogram=FALSE` to silence this warning.
summary_table
Socio-demographic variables

Unique (#)

Missing (%)

Mean

SD

Min

Median

Max

rlgblg

4

0

1.5

0.6

1.0

2.0

8.0

domicil

8

0

2.9

1.0

1.0

3.0

9.0

Mission 6

set_flextable_defaults(fonts_ignore=TRUE)

print(summary_table_v2, preview = "word")
## a flextable object.
## col_keys: ` `, `Unique (#)`, `Missing (%)`, `Mean`, `SD`, `Min`, `Median`, `Max` 
## header has 1 row(s) 
## body has 3 row(s) 
## original dataset sample: 
##                                                         Unique (#) Missing (%)
## 1                                     How happy are you         10          65
## 2 Important to be rich, have money and expensive things         10           1
## 3                                  Trust in Politicians         12           1
##   Mean  SD Min Median  Max
## 1  6.8 6.1 0.0    6.0 88.0
## 2  4.4 1.3 1.0    5.0  9.0
## 3  3.5 2.3 0.0    4.0 10.0

Mission 7

df$year <- NA
replacements <- c(2002, 2004, 2006, 2008, 2010, 2012, 2014, 2016, 2018, 2020)
for(i in 1:10){
  df$year[df$essround == i] <- replacements[i]
}

uk_data <- df

uk_data_table_subset <- uk_data %>%
  mutate(
    rlgblg = ifelse(rlgblg== 2, 0, ifelse(rlgblg %in% c(7, 8, 9), NA, rlgblg)), 
     domicil = ifelse(domicil %in% c(7, 8, 9), NA, domicil) 
  )

avg_domicil_by_year <- aggregate(domicil ~ year + rlgblg, data=uk_data_table_subset, mean)


p1 <- ggplot(avg_domicil_by_year, aes(x=year, y=domicil, color=as.factor(rlgblg))) + 
  geom_line(aes(group=rlgblg)) +
  labs(title="Mean of Interested in Politics", 
       subtitle = "for those Worked rural or urban in UK",
       x="Survey Year", 
       y="Average Interested in Politics") +
  scale_color_discrete(name="Worked in Political in rural/ urban", labels=c("No", "Yes"))+
  theme_minimal()

p1