possibly()

Intermediate Functional Programming with purrr

Colin Fay

Data Scientist & R Hacker at ThinkR

O funkci possibly()

possibly() vytvoří funkci, která vrátí buď:

  • výsledek

  • hodnotu otherwise

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

possible_sum("a")
0

"nop"
Intermediate Functional Programming with purrr

Použití possibly()

possibly() může vrátit:

  • Logickou hodnotu
ps <- possibly(sum, FALSE)
ps("a")
FALSE
  • Hodnotu NA
ps <- possibly(sum, NA)
ps("a")
NA

 

  • Řetězec
ps <- possibly(sum, "nope")
ps("a")
"nope"
  • Číslo
ps <- possibly(sum, 0)
ps("a")
0
Intermediate Functional Programming with purrr

Pojďme si procvičit!

Intermediate Functional Programming with purrr

Preparing Video For Download...