Chapter 8 Midpoint
Let’s try to solidify our understanding of the past concepts. These exercises will require the “ANES_isolate.tsv” file that accompanies Handout 5.
Don’t forget:
library(tidyverse)
<- read.table("ANES_isolate.tsv") data_isolate
8.1 Exercise 1
Compare the ages of females and non-females. Generate a (single) dataframe that contrasts the mean, median, max, and min ages between the two groups.
Solution:
<- data_isolate %>%
data_summary group_by(female) %>%
summarize(mean_age = mean(age),
median_age = median(age),
max_age = max(age),
min_age = min(age))
data_summary
## # A tibble: 2 × 5
## female mean_age median_age max_age min_age
## <int> <dbl> <dbl> <int> <int>
## 1 0 44.9 43 99 17
## 2 1 45.5 43 98 17
8.2 Exercise 2
We want to learn more about moderates (partyid = 4
) and their demographics. Create a dataframe that has only moderates, and include only the columns age
, income
, female
, and minority
. Then, add a new column, retire_age
, that is = 1
if an individual’s age is >= 65
.
Solution:
<- data_isolate %>%
data_moderates filter(partyid == 4) %>%
select(c(age, income, female, minority)) %>%
mutate(retire_age = ifelse(age >= 65, 1, 0))
head(data_moderates)
## age income female minority retire_age
## 3701 43 2 1 1 0
## 3709 25 2 1 0 0
## 3720 52 3 0 0 0
## 3721 33 3 0 0 0
## 3726 32 4 0 0 0
## 3754 62 1 1 0 0