Get startedGet started for free

Soviet space dogs, the dog perspective

You'll be working on an pre-processed sample of the USSR space dogs database compiled by Duncan Geere and pre-loaded for you as space_dogs_df. Each of the 42 rows in this dataset represents a test rocket launch which had one or two very brave dogs on board.

Your goal is to reshape this dataset so that for each launch, each dog has a row.

The challenge is that in the column headers (name_1, name_2, gender_1, and gender_2), the part before the _ separator can point to two different variables (name and gender), while the second part always points to the dog ID (1st or 2nd dog).

Laika

Laika, the first animal to orbit Earth in 1957

This exercise is part of the course

Reshaping Data with tidyr

View Course

Exercise instructions

  • As the first argument to pivot_longer(), pass the columns to pivot (name_1, name_2, gender_1, and gender_2).
  • Complete the names_to argument so that the first part of the column headers are reused.
  • Make sure NA values are dropped since not all rockets had two dogs.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

space_dogs_df %>% 
  pivot_longer(
    # Add the columns to pivot
    ___,
    names_sep = "_",
    # Complete the names_to argument to re-use the first part of the column headers
    names_to = c(___,  "dog_id"),
    # Make sure NA values are dropped
    ___
  )
Edit and Run Code