Mission 1

Launch a new R project and R markdown file. Name it “Lastname_Firstname_Project_202”. Set up your environment with packages you will use.

setwd("~/Desktop/Yr 2 University/Semester 1/SOC 202")

getwd()

library(fst)

ess <- read_fst("All-ESS-Data.fst")

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

new_packages <- packages[!(packages %in% installed.packages()[,"Package"])]
if(length(new_packages)) install.packages(new_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

Mission 2

Filter to your country of interest and save the dataset.

Italy_data <- ess %>% 
  filter(cntry == "IT") 

#save dataset

write_fst(Italy_data, "~/Desktop/Yr 2 University/Semester 1/SOC 202/Italy_data.fst") 

Mission 3

Clean your environment and load in the filtered dataset.

#I cleaned my environment manually

Mission 4

Produce and save a data summary output (using data summary skim) for potential outcomes of interest on a similar scale (e.g., 0-10, or 1 to 6, or binary). Add a title. You can do so while coding (explore package information for flextable and/or modelsummary) or add it directly in the word file. Title should be something like: Table 1: Descriptive Statistics for outcome variables. You can alter the title as you see fit.

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


# make sure you know your current folder path. you can do getwd() to check

Italy_data_table_subset <- Italy_data %>%
  mutate(
    clsprty = ifelse(clsprty == 2, 0, ifelse(clsprty %in% c(7, 8, 9), NA, clsprty)), 
    stfdem = ifelse(stfdem %in% c(77, 88, 99), NA, stfdem), 
    trstplt = ifelse(trstplt %in% c(77, 88, 99), NA, trstplt) 
  )

summary_table <- datasummary_skim(Italy_data_table_subset %>% select(clsprty, stfdem, trstplt), 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.
## 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

Unique (#)

Missing (%)

Mean

SD

Min

Median

Max

clsprty

3

7

0.4

0.5

0.0

0.0

1.0

stfdem

12

4

4.7

2.3

0.0

5.0

10.0

trstplt

12

2

2.8

2.3

0.0

3.0

10.0

Mission 4 continued?

Italy_data_v2 <- Italy_data_table_subset %>%
  rename(
    `Feeling Close to a Party` = clsprty,
    `Satisfaction with Democracy` = stfdem,
    `Trust in Politicians` = trstplt
  )

# Check the table again

summary_table_v2 <- datasummary_skim(Italy_data_v2 %>% select(`Feeling Close to a Party`,`Satisfaction with Democracy`, `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

Feeling Close to a Party

3

7

0.4

0.5

0.0

0.0

1.0

Satisfaction with Democracy

12

4

4.7

2.3

0.0

5.0

10.0

Trust in Politicians

12

2

2.8

2.3

0.0

3.0

10.0

#save above table as PDF

#to save it as a PDF

flextable::save_as_docx(summary_table_v2, path = "Table 1: Summary Table of varibales clsprty, stfdem and trstplt for Italy.docx",
                        width = 7.0, height = 7.0)

set_flextable_defaults(fonts_ignore=TRUE)

print(summary_table_v2, preview = "pdf")
## 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 (%) Mean  SD Min Median  Max
## 1    Feeling Close to a Party          3           7  0.4 0.5 0.0    0.0  1.0
## 2 Satisfaction with Democracy         12           4  4.7 2.3 0.0    5.0 10.0
## 3        Trust in Politicians         12           2  2.8 2.3 0.0    3.0 10.0

Mission 5

Produce and save a data summary output (using data summary skim) for socio-demographic variables. Add a title. You can do so while coding (explore package information for flextable and/or modelsummary) or add it directly in the word file. Title should be something like: Table 1: Descriptive Statistics for socio-demographic variables. You can alter the title as you see fit.

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


Italy_data_table_subsetm5 <- Italy_data %>%
  mutate(
    domicil = ifelse(domicil %in% c(7,8,9), NA, domicil),
    gndr = ifelse(gndr == 2, 0, ifelse(gndr %in% c(7, 8, 9), NA, gndr)),
    lrscale = ifelse(lrscale %in% c(77, 88, 99), NA, lrscale),
  )

summary_tablem5 <- datasummary_skim(Italy_data_table_subset %>% select(domicil, gndr, lrscale), 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_tablem5

Unique (#)

Missing (%)

Mean

SD

Min

Median

Max

domicil

8

0

3.2

1.0

1.0

3.0

9.0

gndr

3

0

1.5

0.6

1.0

2.0

9.0

lrscale

14

0

26.4

35.0

0.0

6.0

99.0

#Mission 5 Continued

Italy_data_m5v2 <- Italy_data_table_subset %>%
  rename(
    `Place of Residence` = domicil,
    `Gender` = gndr,
    `Placement on left right scale` = lrscale
  )

summary_table_m5v2 <- datasummary_skim(Italy_data_m5v2 %>% select(`Place of Residence`,`Gender`, `Placement on left right scale`), 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_m5v2

Unique (#)

Missing (%)

Mean

SD

Min

Median

Max

Place of Residence

8

0

3.2

1.0

1.0

3.0

9.0

Gender

3

0

1.5

0.6

1.0

2.0

9.0

Placement on left right scale

14

0

26.4

35.0

0.0

6.0

99.0

#save this table as Pdf

flextable::save_as_docx(summary_table_m5v2, path = "Table 2: Descriptive Statistics for socio-demographic variables domicil,gndr,lrscale for Italy.docx",
                        width = 7.0, height = 7.0)

set_flextable_defaults(fonts_ignore=TRUE)