Exercise 6: tidycomm

After working through Exercise 6, you’ll…

  • have practiced the most important functions of tidycomm
  • have decided whether you prefer tidycomm or dplyr to solve some problems in R

First, you’ll need to load the statwars_data:

library(dplyr)

starwars_data <- dplyr::starwars

Please, also activate the tidycomm package:

library(tidycomm)

Task 1

How many characters are contained in the starwars_data that have no hair_color, i.e. hair_color that are equal to “none”? Solve this task using tidycomm::tab_frequencies() and then keeping only the row that you are interested in by combining it with dplyr::filter().

Task 2

Replace all values of hair_color that are equal to “none” with NA, i.e., define them as missings. Remember to check the overwrite argument. :)

Task 3

Reverse the variable mass, and call the new variable “skinnyness”. Reverse the variable height, and call the new variable “tinyness”.

Task 4

Scale skinnyness to a new range from 0 to 1. The function should automatically create a variable called skinnyness_0to1. Display only the variables name, skinnyness, and skinnyness_0to1.

Task 5

Create two new variables: skinnyness_centered with center_scale() and skinnyness_z with z_scale(). Display all the variables name, skinnyness, skinnyness_0to1, skinnyness_centered, skinnyness_z.

Task 6

Visualize the variables skinnyness, skinnyness_0to1, skinnyness_centered, skinnyness_z using tidycomm::tab_frequencies) and tidycomm::visualize().

Task 7

Categorize skinnyness into three groups: - “Heavy” for values ≤ 1200
- “Slim” for values > 1200 and ≤ 1300
- “Skinny” for values > 1300

Use tidycomm::categorize_scale(). The function should automatically create a variable called skinnyness_cat. Display the variables name, skinnyness, and skinnyness_cat.

Task 8

Use tidycomm::recode_cat_scale() to recode skinnyness_cat like this:

  • "Heavy""high_mass"
  • "Slim""medium_mass"
  • "Skinny""low_mass"

Do not overwrite the original variable. Instead, let recode_cat_scale() create a new variable automatically.
Display the variables name, skinnyness, skinnyness_cat, and the recoded variable.

Task 9

Create a mean index called smallness_index using the variables skinnyness and tinyness. Then display only the mean index and the original variables that were used to create it.

Task 10

Now create a sum index with the same variables and call it smallness_sum_index. Then display only the mean AND sum index and the original variables that were used to create it.

Task 11

Compute reliability estimates for the mean index smallness_index.