LoslegenKostenlos starten

safely() replace with NA

If you map() over a list, and one of the elements does not have the right data type, you will not get the output you expect. Perhaps you are trying to do a mathematical operation on each element, and it turns out one of the elements is a character - it simply won't work.

If you have a very large list, figuring out where things went wrong, and what exactly went wrong can be hard. That is where safely() comes in; it shows you both your results and where the errors occurred in your map() call.

Diese Übung ist Teil des Kurses

<Kurs>Foundations of Functional Programming with purrr</Kurs>
Kurs ansehen

Interaktive praktische Übung

Versuche dich an dieser Übung, indem du diesen Beispielcode vervollständigst.

# Map safely over log
a <- list(1, "I can", 10, 0, "purrr") %>%
      map(___(___, otherwise = NA_real_)) %>%
    # Transpose the result
      transpose() 
Code bearbeiten und ausführen