Chapter 8 Base-R Quiz

8.1 Instruction on the quiz

  • Quiz 1 consists of 5 multiple-choice problems.

  • Solve each of five problems in your own R markdown file.

    • You DON’T need to submit any R markdown file for Quiz 1.
  • Once you solve all problems, then go to cyber campus and find Quiz 1.

  • Please mark the right answer to each of the 5 multiple choice problems.

  • Some quiz problems need the round() function, which will return the integer part of the provided number. For quiz responses, please round your answers to the two decimal place.

# round to the two decimal place
round(3.141592, digits = 2)
## [1] 3.14

8.2 Dataset: bfi is a dataset in the psych package containing 25 personality self report items taken from the International Personality Item Pool. In the dataset,

  1. gender represents gender (Males = 1, Females = 2),
  2. education represent educational attainment (1 = HS, 2 = finished HS, 3 = some college, 4 = college graduate 5 = graduate degree),
  3. age represents age in years, and
  4. E3 is one of the five items that measures extraversion (“Know how to captivate people”).
# You need to install and load the `psych` package to use the `bfi` dataset.
library(psych)

Let’s create three vectors for gender, education, and age variables in the diamonds dataset.

gender <- bfi$gender
education <- bfi$education
age <- bfi$age
E3 <- bfi$E3

8.3 Quiz problem 1

What is the type of each of these vectors (character, integer, double, factor)?

Answer: Integer

8.4 Quiz problem 2

What proportion of the sample is females?

Answer: 0.67

# Base-R 
round(sum(gender == 2)/length(gender), digits = 2)
## [1] 0.67
# tidyverse 
bfi %>%
  group_by(gender) %>%
  summarise(n = n()) %>%
  mutate(p = n/sum(n))
## `summarise()` ungrouping output (override with `.groups` argument)
## # A tibble: 2 x 3
##   gender     n     p
##    <int> <int> <dbl>
## 1      1   919 0.328
## 2      2  1881 0.672

8.5 Quiz problem 3

What is the sample mean difference in E3 between males (gender = 1) younger than (<) 26 and females (gender = 2) younger than (<) 26? That is, mean of E3 for males younger than 26 - mean of E3 for females younger than 26.

Answer: -0.07

# Base-R
round(mean(E3[gender == 1 & age < 26], na.rm = TRUE) - mean(E3[gender == 2 & age < 26], na.rm = TRUE), digits = 2)
## [1] -0.07
# tidyverse
bfi %>%
  filter(age < 26) %>%
  group_by(gender) %>%
  summarise(m = mean(E3, na.rm = TRUE))
## `summarise()` ungrouping output (override with `.groups` argument)
## # A tibble: 2 x 2
##   gender     m
##    <int> <dbl>
## 1      1  3.93
## 2      2  4.00

8.6 Quiz problem 4

In the bfi dataset, how many males with age 16 and highschool degree are there?

Answer: 6

sum(gender == 1 &  age == 16 & education == 1, na.rm = TRUE)
## [1] 6
bfi %>%
  filter(gender == 1, age == 16, education == 1)
##   A1 A2 A3 A4 A5 C1 C2 C3 C4 C5 E1 E2 E3 E4 E5 N1 N2 N3 N4 N5 O1 O2 O3 O4 O5
## 1  1  6  3  4  2  5  6  5  1  1  2  3  4  5  4  4  1  3  2  5  5  2  6  6  1
## 2  3  5  6  6  5  4  4  3  3  3  2  3  5  6  4  1  4  3  2  2  3  5  4  5  1
## 3  3  5  4  4  5  5  4  4  4  6  4  4  4  4  5  2  2  6  6  5  6  1  5  6  3
## 4  5  5  5  5  5  5  6  5  5 NA  5  4  6  5  6  5  6  5  4  5 NA  6  5  5  6
## 5  3  5  4  5  6  5 NA NA  4  5  2  1  5  6  4  1  1  5  4  1  6  1  5  6  4
## 6  5  6  6  1  6  5  6  5 NA  6  1  5  5  5  5  2  3  6  6  4  6  2  5  6  2
##   gender education age
## 1      1         1  16
## 2      1         1  16
## 3      1         1  16
## 4      1         1  16
## 5      1         1  16
## 6      1         1  16

8.7 Quiz problem 5

Using the bfi data set, what is the count of 42 year old females?

Answer: 21

# Base R
sum(gender == 2 & age == 42)
## [1] 21
# tidyverse
bfi %>%
  filter(gender == 2, age == 42)
##    A1 A2 A3 A4 A5 C1 C2 C3 C4 C5 E1 E2 E3 E4 E5 N1 N2 N3 N4 N5 O1 O2 O3 O4 O5
## 1   2  5  5  5  3  5  5  5  1  4  4  4  3  3  5  4  4  4  4  2  6  1  5  4  1
## 2   1  6  5  6  6  6  6  6  1  1  1  1  4  6  5  1  1  1  1  1  6  1  6  6  1
## 3   1  4  4  6  4  3  4  2  2  2  2  4  4  4  4  4  4  5  4  4  3  4  3  5  3
## 4   2  6  5  5  4  5  5  5 NA  2  1  4  4  6  5  1  4  5  2  6  4  4  4  5  2
## 5   3  6  6  6  5  6  5  6  1  2  3  1  5  6  5  1  3  2  1  1  5  1  5  6  1
## 6   1  6  6 NA  5  1  6  6  1  1  2  5  4  4  6  5  5  6  4  3  5  1  2  5  1
## 7   1  6  6  6  1  5  2  5  2  4  2  4  4  6  4  1  1  1  1  2  5  3  1  2  3
## 8   2  6  5  2  5  5  2  4  1  3  1  1  5  6  5  3  2  1  2  1  4  3  3  4  2
## 9   1  6  5  5  6  5  3  5  1  4  1  2  4  5  4  2  4  2  2  2  5  2  5  5  2
## 10  4  5 NA  6  5  6  5  4  3  4  3  4  4  5  5  5  5  5  4  3  5  3  5  5  4
## 11  1  6  6  4  6  4  5  5  2  2  1  1  4  6  5  2  2  2  4  2  5  4  5  5  2
## 12  1  6  6  6  6  5  5  4  2  2  2  4  4  5  3  3  4  2  3  3  5  2  4  6  2
## 13  1  6  6  6  5  6  5  6  1  1  4  1  4  5  6  1  2  2  3  1  5  2  6  6  2
## 14  5  3  2  5  3  6  6  4  1  6  6  6  2  1  3  4  6  5  6  6  4  6  2  6  1
## 15  1  6  6  6  5  5 NA  5  1  2  3  2  3  3  2  2  2  1  2  4  3  2  5  6  1
## 16  1  6  6  6  6  6  6  4  1  4  1  2  5  6  5  2  3  3  2  4  5  3  4  5  1
## 17  3  4  4  6  3  4  6  5  3  4  4  4  3  3  4  3  5  4  3  3  4  5  3  4  3
## 18  1  5  5  6  5  3  2  3  4  4  1  1  5  6  4  3  4  5 NA  4  4  3  5  4  2
## 19  1  5  5  6  6  5  5  5  1  2  1  1  5  6  5  1  1  1  1  2  5  1  5  4  2
## 20  1  6  6  5  6  4  4  5  2  2  2  2  5  5  5  1  1  2  1  2  5  1  5  3  4
## 21  2  5  5  4  6  6  5  5  1  1  5  1  1  3  6  4  5  3  2  1  5  2  1  4  2
##    gender education age
## 1       2         5  42
## 2       2         3  42
## 3       2         2  42
## 4       2         2  42
## 5       2         3  42
## 6       2         3  42
## 7       2         2  42
## 8       2         2  42
## 9       2         5  42
## 10      2         3  42
## 11      2         5  42
## 12      2         3  42
## 13      2         2  42
## 14      2         2  42
## 15      2         3  42
## 16      2         5  42
## 17      2         1  42
## 18      2         4  42
## 19      2         3  42
## 20      2         4  42
## 21      2         2  42