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.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00

Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.

packages <- c("tidyverse", "infer", "fst") # add any you need here

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
## [[1]]
##  [1] "lubridate" "forcats"   "stringr"   "dplyr"     "purrr"     "readr"    
##  [7] "tidyr"     "tibble"    "ggplot2"   "tidyverse" "stats"     "graphics" 
## [13] "grDevices" "utils"     "datasets"  "methods"   "base"     
## 
## [[2]]
##  [1] "infer"     "lubridate" "forcats"   "stringr"   "dplyr"     "purrr"    
##  [7] "readr"     "tidyr"     "tibble"    "ggplot2"   "tidyverse" "stats"    
## [13] "graphics"  "grDevices" "utils"     "datasets"  "methods"   "base"     
## 
## [[3]]
##  [1] "fst"       "infer"     "lubridate" "forcats"   "stringr"   "dplyr"    
##  [7] "purrr"     "readr"     "tidyr"     "tibble"    "ggplot2"   "tidyverse"
## [13] "stats"     "graphics"  "grDevices" "utils"     "datasets"  "methods"  
## [19] "base"
ess <- read_fst("All-ESS-Data.fst")

#Task 1

switz_data <- ess %>%
  # Filter for France
  filter(cntry == "CH") %>%
  
  # Recoding clsprty and cleaning trstplt (will use later)
  mutate(
    clsprty = case_when(
      clsprty == 1 ~ "Yes",  
      clsprty == 2 ~ "No",   
      clsprty %in% c(7, 8, 9) ~ NA_character_,  
      TRUE ~ as.character(clsprty)  
    ),
    trstplt = ifelse(trstplt %in% c(77, 88, 99), NA, trstplt),
    
    # Recoding education for rounds 1-4
    edulvla = case_when(
      essround < 5 & edulvla == 55 ~ NA_real_,
      TRUE ~ edulvla
    ),
    
    # Re-coding education for rounds 5 and later
    edulvlb = case_when(
      essround >= 5 & edulvlb == 5555 ~ NA_real_,
      TRUE ~ edulvlb
    ),
    
    # Create educ_level column
    educ_level = case_when(
      essround < 5 & edulvla == 5 ~ "BA",
      essround >= 5 & edulvlb > 600 ~ "BA",
      TRUE ~ "No BA"
    ),
    
    # Recoding gender
    gndr = case_when(
      gndr == 1 ~ "Male",
      gndr == 2 ~ "Female",
      gndr == 9 ~ NA_character_,
      TRUE ~ as.character(gndr)
    )
  )
switzerland_data <- ess %>%
  filter(cntry == "CH") %>%
  mutate(
    gndr = recode(gndr, `1` = "Male", `2` = "Female", `9` = NA_character_),
    lrscale = case_when(
      lrscale %in% 0:3 ~ "Left",
      lrscale %in% 7:10 ~ "Right",
      lrscale %in% c(4, 5, 6, 77, 88, 99) ~ NA_character_,
      TRUE ~ as.character(lrscale)
    )
  )

#Task 2

nl_data <- ess %>%
  filter(cntry == "NL")

nl_data <- nl_data %>%
  mutate(geo = recode(as.character(domicil), 
                      '1' = "Agree Strongly", 
                      '2' = "Agree", 
                      '3' = "Neither Agree Nor Disagree", 
                      '4' = "Disagree", 
                      '5' = "Disagree Strongly",
                      '7' = NA_character_,
                      '8' = NA_character_,
                      '9' = NA_character_))

nl_data <- ess %>%
  filter(cntry == "NL")

nl_data <- nl_data %>%
  mutate(gincdif = recode(gincdif, `2` = 0, 
                          `7` = NA_real_, 
                          `8` = NA_real_, 
                          `9` = NA_real_),
         geo = recode(as.character(domicil), 
                      '1' = "Agree Strongly", 
                      '2' = "Agree", 
                      '3' = "Neither Agree Nor Disagree", 
                      '4' = "Disagree", 
                      '5' = "Disagree Strongly",
                      '7' = NA_character_,
                      '8' = NA_character_,
                      '9' = NA_character_))


print(nl_data$stat)
## NULL

Task 3

For my research question, I want to analyze how education levels affect thoughts on immigration using the variable imbgeco (is immigration good or bad for a county’s economy?)