BaşlayınÜcretsiz Başlayın

Convert data to numeric with purrr

In the sw_people dataset, some of the Star Wars characters have unknown heights. If you want to do some data exploration and determine how character height differs depending on their home planet, you need to write your code so that R understands the difference between heights and missing values. Currently, the missing values are entered as "unknown", but you would like them as NA. In this exercise, you will combine map() and ifelse() to fix this issue.

Bu egzersiz

Foundations of Functional Programming with purrr

kursunun bir parçasıdır
Kursu Görüntüle

Egzersiz talimatları

  • Load the sw_people dataset.
  • Map over sw_people and pull out "height".
  • Then map over the output and if an element is labeled as "unknown" change it to NA, otherwise, convert the value into a number with as.numeric().

Uygulamalı interaktif egzersiz

Bu örnek kodu tamamlayarak bu egzersizi bitirin.

# Load sw_people data
data(___)

# Map over sw_people and pull out the height element
height_cm <- map(___, ___) %>%
  map(function(x){
    ifelse(___ == "unknown",NA,
    as.numeric(___))
})
Kodu Düzenle ve Çalıştır