2.6 File examples
To write a comment, put a hashtag (#) at any point in a line of code. Anything that follows the hashtag in the line will be interpreted by R as a comment, and will not be executed. Leaving notes for yourself is a good programming practice.
2.6.1 WVS data
# Load packages
library(tidyverse)
library(summarytools)
library(haven)
# Call the file (You put it in the project folder!)
# This uses the "haven" package to read the file in.
ethiopia <- read_sav("WVS_Wave_7_Ethiopia_Spss_v1.5 2.sav", user_na = TRUE) %>%
as_factor()
## Manage the file by collapsing NAs and eliminating unneeded categories
## Don't load this package until now!
library(sjlabelled)
ethiopia <- set_na(ethiopia, na = c("No answer", "Don´t know", "Other missing; Multiple answers Mail (EVS)", "Not asked"), drop.levels = TRUE)
# Run frequencies of your variables
# Names are yourDataset$variableName
freq(ethiopia$Q5)
freq(ethiopia$Q34)
freq(ethiopia$Q11)
freq(ethiopia$H_URBRURAL)
freq(ethiopia$Q261)
freq(ethiopia$G_TOWNSIZE2)
# Save the file as .RData to make it easy to call the file next time.
save(ethiopia, file = "ethiopia.RData")

2.6.2 States data
# Load packages
library(tidyverse)
library(summarytools)
library(haven)
# Call the file (You put it in the project folder!)
# This uses the "haven" package to read the file in.
myStateData <- read_excel("cspp_data_2021-02-14.xlsx", na = "NA")
View(my_usstate_data)
## Filter the data to select only the year you want
my_usstate_data_2011 <- my_usstate_data %>%
filter(year == 2011)
freq(my_usstate_data_2011$region)
freq(my_usstate_data_2011$gini_coef)
freq(my_usstate_data_2011$povrate)
freq(my_usstate_data_2011$pctfemaleleg)
freq(my_usstate_data_2011$pctwomenearn)
freq(my_usstate_data_2011$evangelical_pop)
# Save the file as .RData to make it easy to call the file next time.
save(myStateData, file = "MyStateData.RData")
2.6.3 Gapminder data
# Load packages
library(tidyverse)
library(summarytools)
### Your file has already been created click on it in the lower right quadrant in the files pane.
### You can also open it by using the load command
load("~/Dropbox/R Projects/gapminder_f21_POLS200/my_gapminder_data.Rdata")
View(my_gapminder_data)
## Filter the data to select only the year you want
my_gapminder_data_2003 <- my_gapminder_data %>%
filter(year == 2003)
freq(my_gapminder_data_2003$world_6region)
freq(my_gapminder_data_2003$income_groups)
freq(my_gapminder_data_2003$mortalityRate)
freq(my_gapminder_data_2003$studentSpendingPerCapita)
# Save the file filtered file as .RData to make it easy to call the file next time.
save(my_gapminder_data_2003, file = "my_gapminder_data_2003.RData")
