possibly()

Fortgeschrittene funktionale Programmierung mit purrr

Colin Fay

Data Scientist & R Hacker at ThinkR

Über possibly()

`possibly() erstellt eine Funktion, die entweder Folgendes zurückgibt:

  • das Ergebnis

  • den Wert von otherwise

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

possible_sum("a")
0

"nop"
Fortgeschrittene funktionale Programmierung mit purrr

possibly() verwenden

possibly() kann zurückgeben:

  • Einen Logikwert
ps <- possibly(sum, FALSE)
ps("a")
FALSE
  • Ein NA
ps <- possibly(sum, NA)
ps("a")
NA

 

  • Einen Text (character)
ps <- possibly(sum, "nope")
ps("a")
"nope"
  • Eine Zahl
ps <- possibly(sum, 0)
ps("a")
0
Fortgeschrittene funktionale Programmierung mit purrr

Lass uns üben!

Fortgeschrittene funktionale Programmierung mit purrr

Preparing Video For Download...