possibly()

Programmation fonctionnelle intermédiaire avec purrr

Colin Fay

Data Scientist & R Hacker at ThinkR

À propos de possibly()

possibly() crée une fonction qui renvoie soit :

  • le résultat

  • la valeur de otherwise

library(purrr)
possible_sum <- possibly(sum, otherwise = "nop")
possible_sum(1)

possible_sum("a")
0

"nop"
Programmation fonctionnelle intermédiaire avec purrr

Utiliser possibly()

possibly() peut renvoyer :

  • Un booléen
ps <- possibly(sum, FALSE)
ps("a")
FALSE
  • Un NA
ps <- possibly(sum, NA)
ps("a")
NA

 

  • Une chaîne de caractères
ps <- possibly(sum, "nope")
ps("a")
"nope"
  • Un nombre
ps <- possibly(sum, 0)
ps("a")
0
Programmation fonctionnelle intermédiaire avec purrr

Passons à la pratique !

Programmation fonctionnelle intermédiaire avec purrr

Preparing Video For Download...