Knowledge Quiz: Tutorial 2

Note
  • Click the check-mark button to check your answer.
  • Click the question-mark button to see an explanation of the solution.

Which dplyr command creates a mean-centered version of age called age_mc in the data frame bfi?

  • FALSE. Divides by the mean, normalization, not centering.
  • FALSE. Correct syntax but mean(bfi$age) is less common than mean(age) within mutate().
  • FALSE. Missing the data frame to mutate.
  • FALSE. scale() standardizes (centers and scales), not just centers.
  • TRUE. This correctly creates a mean-centered version of age.

Which of the following use across() correctly to compute row means for all items from "A1" to "A5" and store the result in a new variable agree?

  • FALSE. This will not work as intended because rowMeans cannot directly take the result of across() without proper handling.
  • TRUE. This is also a correct usage of across() to select the columns and compute row means with NA handling.
  • TRUE. This is the correct way to use across() to select columns A1 to A5 and compute their row means while handling NA values.
  • FALSE. This does not use across() and will result in an error since rowMeans cannot directly take column ranges.
  • FALSE. This is incorrect because across() is not designed to be used this way for row-wise operations.

Which commands correctly rename variables in the bfi dataset using rename() or rename_with() from dplyr?

{. .cell-code} head(bfi)

      A1 A2 A3 A4 A5 C1 C2 C3 C4 C5 E1 E2 E3 E4 E5 N1 N2 N3 N4 N5 O1 O2 O3 O4
61617  2  4  3  4  4  2  3  3  4  4  3  3  3  4  4  3  4  2  2  3  3  6  3  4
61618  2  4  5  2  5  5  4  4  3  4  1  1  6  4  3  3  3  3  5  5  4  2  4  3
61620  5  4  5  4  4  4  5  4  2  5  2  4  4  4  5  4  5  4  2  3  4  2  5  5
61621  4  4  6  5  5  4  4  3  5  5  5  3  4  4  4  2  5  2  4  1  3  3  4  3
61622  2  3  3  4  5  4  4  5  3  2  2  2  5  4  5  2  3  4  4  3  3  3  4  3
61623  6  6  5  6  5  6  6  6  1  3  2  1  6  5  6  3  5  2  2  3  4  3  5  6
      O5 gender education age   gm
61617  3      1        NA  16 <NA>
61618  3      2        NA  18 <NA>
61620  2      2        NA  17 <NA>
61621  5      2        NA  17 <NA>
61622  3      1        NA  17 <NA>
61623  1      2         3  21 <NA>
  • FALSE. Incorrect order of arguments.
  • FALSE. Incorrect function usage.
  • TRUE. Renames one variable.
  • FALSE. Incorrect syntax for renaming.
  • TRUE. Renames first three variables to uppercase.

Write one line of dplyr code that creates a new variable maturity in the bfi data frame, that is equal to "minor" if age is less than 18 and is "adult" if age is greater than or equal to 18

The required R code is: bfi <- mutate(bfi, maturity = case_when(age < 18 ~ "minor", age >= 18 ~ "adult"))

Back to top