Skills Lab 03: Logic of piping |>

Author

MS

Google doc: https://bit.ly/skills-lab-03

Functions and arguments

Task 1

  1. Run the chunk below to create and object called my_numbers
  2. Look up the help documentation for the round() function
  3. Round my_numbers to 2 decimal places.
my_numbers <- c(2.6822919, 1.8485851, 1.0039014, 1.9612068, 0.5475432)

Look up documentation:

# run this in the console: 
help(round)

Round to 2 decimal places

round(x = my_numbers, digits = 2)
[1] 2.68 1.85 1.00 1.96 0.55

Using the pipe

Task 2

  1. Round my numbers to 2 decimal places by piping my_numbers into the round() function.
  2. Round my numbers to 2 decimal places by piping number 2 into the round() function.
my_numbers |> round(????)
2 |> round(????)
my_numbers |> round(x = _, digits = 2)
2 |> round(x = my_numbers, digits = _)
[1] 2.68 1.85 1.00 1.96 0.55

Connecting functions with a pipe

Task 3

  1. Run the code below to read in spotify_data.
  2. Without using the pipe change the dataset so that:
    • It only contains rows with tracks in the F Major key
    • It only contains columns with track name, artists name, and year of release
    • Save the amended dataset into an object called spotify_edited using the assignment operator
spotify_data <- readr::read_csv("data/tutorial_03_data.csv")
spotify_edited <- dplyr::filter(spotify_data, key == "F", mode == "Major")
spotify_edited <- dplyr::select(spotify_edited, track_name, `artist(s)_name`, released_year)

Task 4

Change the code from the previous task into a pipeline.

spotify_edited <- spotify_data |> 
  dplyr::filter(.data = _, key == "F", mode == "Major") |> 
  dplyr::select(.data = _, track_name, `artist(s)_name`, released_year)

and this will also work:

spotify_edited <- spotify_data |> 
  dplyr::filter(key == "F", mode == "Major") |> 
  dplyr::select(track_name, `artist(s)_name`, released_year)

Task 5

Using the pipe, complete the following tasks.

  1. Amend the spotify_data so that:
    • It only contains columns with track name, release year, and number of spotify charts
    • Create a new column in spotify_data called score. score can be computed by diving the number of spotify charts by the number of playlists.
    • Save this new version of the dataset into an object called spotify_new

If you try to follow the instructions step by step, you’ll get an error:

spotify_new <- spotify_data |> 
  dplyr::select(track_name, released_year, in_spotify_charts) |> 
  dplyr::mutate(
    score = in_spotify_charts/in_spotify_playlists
  )
Error in `dplyr::mutate()`:
ℹ In argument: `score = in_spotify_charts/in_spotify_playlists`.
Caused by error:
! object 'in_spotify_playlists' not found

This is because in Step 1, we did not select in_spotify_playlists. This means that we cannot use it in Step 2.

If we wanted to complete this task, we would need to either create the new variable first OR select in_spotify_playlists before using mutate()

spotify_new <- spotify_data |> 
  dplyr::select(track_name, released_year, in_spotify_charts, in_spotify_playlists) |> 
  dplyr::mutate(
    score = in_spotify_charts/in_spotify_playlists
  )