Problem 1
my_vector <- c(2, 4, 5, 7, 10)
sum(my_vector)
## [1] 28
Problem 2
my_dat <- tibble(
alpha = 1:5,
beta = 6:10
)
my_dat
## # A tibble: 5 × 2
## alpha beta
## <int> <int>
## 1 1 6
## 2 2 7
## 3 3 8
## 4 4 9
## 5 5 10
Problem 3
summary(my_dat$beta)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 6 7 8 8 9 10
Problem 4
boxplot(dat$faminc_new,
main = "Sylwester Lichosik",
xlab = "Family Income")

Problem 5
problem5 <- dat %>%
filter(gender == 1,
region %in% c(1, 2),
marstat == 1,
newsint == 1)
str(problem5)
## 'data.frame': 75 obs. of 25 variables:
## $ caseid : int 420208067 412948037 411855595 414480371 416806180 414729651 412021973 412348831 412929385 412047867 ...
## $ region : int 2 1 1 1 2 2 1 2 1 2 ...
## $ gender : int 1 1 1 1 1 1 1 1 1 1 ...
## $ educ : int 3 5 6 5 3 2 6 5 5 5 ...
## $ edloan : int 2 1 2 1 1 2 2 2 2 2 ...
## $ race : int 1 1 1 1 1 1 1 1 1 1 ...
## $ hispanic : int 2 2 2 2 2 2 2 2 2 2 ...
## $ employ : int 1 1 1 1 1 5 5 1 1 1 ...
## $ marstat : int 1 1 1 1 1 1 1 1 1 1 ...
## $ pid7 : int 4 1 4 4 6 2 1 1 1 7 ...
## $ ideo5 : int 5 1 3 3 3 1 2 3 1 5 ...
## $ pew_religimp: int 4 4 4 1 2 4 4 2 4 1 ...
## $ newsint : int 1 1 1 1 1 1 1 1 1 1 ...
## $ faminc_new : int 10 11 10 9 10 2 13 8 7 11 ...
## $ union : int 1 2 3 3 1 3 2 2 2 3 ...
## $ investor : int 2 1 1 1 1 2 1 2 1 2 ...
## $ CC18_308a : int 4 4 4 1 2 3 4 4 4 1 ...
## $ CC18_310a : int 2 3 3 3 2 2 3 2 3 2 ...
## $ CC18_310b : int 2 3 3 3 2 3 3 5 3 3 ...
## $ CC18_310c : int 3 3 3 3 3 3 2 5 3 3 ...
## $ CC18_310d : int 2 5 3 3 3 2 2 2 3 3 ...
## $ CC18_325a : int 1 2 1 1 1 2 2 2 2 1 ...
## $ CC18_325b : int 2 2 1 1 1 1 2 1 2 2 ...
## $ CC18_325c : int 1 2 2 1 1 2 2 1 2 2 ...
## $ CC18_325d : int 1 1 1 1 1 1 1 1 2 1 ...
Problem 6
problem6 <- dat %>%
filter(gender == 1,
region %in% c(1, 2),
marstat == 1,
newsint == 1)
table(problem6$investor)
##
## 1 2
## 57 18
Problem 7
problem7 <- dat %>%
mutate(tax_scale = recode(CC18_325a, `1` = 1, `2` = 0) +
recode(CC18_325d, `1` = 1, `2` = 0))
head(problem7$tax_scale, 20)
## [1] 2 1 1 1 2 2 2 1 0 1 0 2 0 1 0 1 1 1 2 1
Problem 8
table(problem7$tax_scale)
##
## 0 1 2
## 130 408 331
Problem 9
problem9 <- dat %>%
group_by(region) %>%
summarise(mean_9 = mean(CC18_308a))
problem9
## # A tibble: 4 × 2
## region mean_9
## <int> <dbl>
## 1 1 2.77
## 2 2 2.76
## 3 3 2.71
## 4 4 3.03
Problem 10
problem10 <- dat %>%
filter(investor == 2, faminc_new >= 5 & faminc_new <= 10) %>%
summarise(
mean_religion = mean(pew_religimp),
median_religion = median(pew_religimp),
sd_religion = sd(pew_religimp)
)
problem10
## mean_religion median_religion sd_religion
## 1 2.325893 2 1.188906
Problem 11
dat %>%
summarise(
mean = mean(faminc_new),
median = median(faminc_new),
sd = sd(faminc_new)
) %>%
knitr::kable(caption = "Summarise Family Income")
Summarise Family Income
| 6.581128 |
6 |
3.247035 |
Problem 12
qplot(pid7, data = dat, geom = "histogram",
xlab = "Seven Point Party ID",
ylab = "Count")
## Warning: `qplot()` was deprecated in ggplot2 3.4.0.
## This warning is displayed once per session.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
