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.
Mission 2 Filter to your country of interest and save the dataset.
setwd("~/Downloads/")
ess <- read_fst("All-ESS-Data.fst")
UK_data <- ess %>%
filter(cntry == "GB")
write_fst(UK_data, "~/Downloads/UK_data.fst")
Mission 3 Clean your environment and load in the filtered dataset.
rm(list=ls()); gc()
## used (Mb) gc trigger (Mb) limit (Mb) max used (Mb)
## Ncells 1263615 67.5 2131018 113.9 NA 2131018 113.9
## Vcells 2152765 16.5 1257028328 9590.4 16384 1361161486 10384.9
df <- read_fst("~/Downloads/UK_data.fst")
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.
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(
ipsuces = ifelse(ipsuces %in% c(7, 8, 9), NA, ipsuces),
iprspot= ifelse(iprspot %in% c(7, 8, 9), NA, iprspot),
ipmodst = ifelse(ipmodst %in% c(7, 8, 9), NA, ipmodst),
)
summary_table <- datasummary_skim(UK_data_table_subset %>% select(ipsuces, iprspot, ipmodst), 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
| Unique (#) | Missing (%) | Mean | SD | Min | Median | Max |
|---|---|---|---|---|---|---|---|
ipsuces | 7 | 3 | 3.5 | 1.4 | 1.0 | 3.0 | 6.0 |
iprspot | 7 | 3 | 3.4 | 1.4 | 1.0 | 3.0 | 6.0 |
ipmodst | 7 | 3 | 2.7 | 1.3 | 1.0 | 2.0 | 6.0 |
UK_data2 <- UK_data_table_subset %>%
rename(
`Important to be successful and that people recognize achievements` = ipsuces,
`Important to get respect from others` = iprspot,
`Important to be humble and modest, not draw attention` = ipmodst
)
summary_table2 <- datasummary_skim(UK_data2 %>% select(`Important to be successful and that people recognize achievements`,`Important to get respect from others`, `Important to be humble and modest, not draw attention`), 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_table2
| Unique (#) | Missing (%) | Mean | SD | Min | Median | Max |
|---|---|---|---|---|---|---|---|
Important to be successful and that people recognize achievements | 7 | 3 | 3.5 | 1.4 | 1.0 | 3.0 | 6.0 |
Important to get respect from others | 7 | 3 | 3.4 | 1.4 | 1.0 | 3.0 | 6.0 |
Important to be humble and modest, not draw attention | 7 | 3 | 2.7 | 1.3 | 1.0 | 2.0 | 6.0 |
flextable::save_as_docx(summary_table2, path = "summary_table2.docx",
width = 7.0, height = 7.0)
set_flextable_defaults(fonts_ignore=TRUE)
print(summary_table2, 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 (#)
## 1 Important to be successful and that people recognize achievements 7
## 2 Important to get respect from others 7
## 3 Important to be humble and modest, not draw attention 7
## Missing (%) Mean SD Min Median Max
## 1 3 3.5 1.4 1.0 3.0 6.0
## 2 3 3.4 1.4 1.0 3.0 6.0
## 3 3 2.7 1.3 1.0 2.0 6.0
Mission 9 Produce a visualization between birth year and your outcome, but only visualize the years for Millennials and Boomers (you can also visualize other birth year ranges, or truncate specifically to say 1950 to 1980 – the idea is to practice visualizing only certain aspects of interest).
setwd("~/Downloads/")
ess <- read_fst("All-ESS-Data.fst")
millennials_data <- ess %>%
filter(cntry == "GB", yrbrn > 1980 & yrbrn < 1997)
boomers_data <- ess %>%
filter(cntry == "GB", yrbrn > 1945 & yrbrn < 1965)
millennials_data_cleaned <- millennials_data %>%
mutate(
ipsuces = ifelse(ipsuces %in% c(7, 8, 9), NA, ipsuces),
iprspot = ifelse(iprspot %in% c(7, 8, 9), NA, iprspot),
ipmodst = ifelse(ipmodst %in% c(7, 8, 9), NA, ipmodst)
) %>%
group_by(yrbrn) %>%
summarise(
avg_ipsuces = mean(ipsuces, na.rm = TRUE),
avg_iprspot = mean(iprspot, na.rm = TRUE),
avg_ipmodst = mean(ipmodst, na.rm = TRUE)
)
boomers_data_cleaned <- boomers_data %>%
mutate(
ipsuces = ifelse(ipsuces %in% c(7, 8, 9), NA, ipsuces),
iprspot = ifelse(iprspot %in% c(7, 8, 9), NA, iprspot),
ipmodst = ifelse(ipmodst %in% c(7, 8, 9), NA, ipmodst)
) %>%
group_by(yrbrn) %>%
summarise(
avg_ipsuces = mean(ipsuces, na.rm = TRUE),
avg_iprspot = mean(iprspot, na.rm = TRUE),
avg_ipmodst = mean(ipmodst, na.rm = TRUE)
)
ggplot() +
geom_point(data = millennials_data_cleaned, aes(x = yrbrn, y = avg_ipsuces ), color = "black", alpha = 0.6) +
geom_point(data = boomers_data_cleaned, aes(x = yrbrn, y = avg_ipsuces ), color = "black", alpha = 0.6) +
geom_smooth(data = millennials_data_cleaned, aes(x = yrbrn, y = avg_ipsuces ), method = "lm", formula = y ~ poly(x, 1), color = "black") +
geom_smooth(data = boomers_data_cleaned, aes(x = yrbrn, y = avg_ipsuces ), method = "lm", formula = y ~ poly(x, 1), color = "black") +
labs(
title = "Average ipsuces Score by Birth Year (Millennials vs. Boomers)",
x = "Year of Birth",
y = "Average ipsuces "
) +
scale_color_manual(values = c("black", "black")) +
theme_bw()