CommencerCommencer gratuitement

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

Cet exercice fait partie du cours

Reshaping Data with tidyr

Afficher le cours

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.

Exercice interactif pratique

Essayez cet exercice en complétant cet exemple de 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
    ___
  )
Modifier et exécuter le code