用 `compose()` 與 `negate()` 組合函式

使用 purrr 的 R 語言中級函式程式設計

Colin Fay

Data Scientist & R Hacker at ThinkR

隨心所欲用 compose()

無限組合:

library(broom)
library(purrr)
clean_aov <- compose(tidy, anova, lm)
clean_aov(Sepal.Length ~ Sepal.Width, data = iris)
# A tibble: 2 x 6
  term           df  sumsq meansq statistic p.value
  <chr>       <int>  <dbl>  <dbl>     <dbl>   <dbl>
1 Sepal.Width     1   1.41  1.41       2.07   0.152
2 Residuals     148 101.    0.681     NA     NA    
使用 purrr 的 R 語言中級函式程式設計

negate()

反轉布林值:

is_not_na <- negate(is.na)
x <- c(1,2,3,4, NA)
is.na(x)
FALSE FALSE FALSE FALSE  TRUE
is_not_na(x)
TRUE  TRUE  TRUE  TRUE FALSE
使用 purrr 的 R 語言中級函式程式設計

搭配 mapper

negates() 與 mapper:

under_hundred <- as_mapper(~ mean(.x) < 100)
not_under_hundred <- negate(under_hundred)
map_lgl(98:102, under_hundred)
TRUE  TRUE FALSE FALSE FALSE
map_lgl(98:102, not_under_hundred)
FALSE FALSE  TRUE  TRUE  TRUE
使用 purrr 的 R 語言中級函式程式設計

題外話:HTTP 代碼

http 狀態碼

出處:[Hypertext Transfer Protocol (HTTP) Status Code Registry ,fixerrorcode.com](https://fixerrorcode.com/http-status-codes/)

使用 purrr 的 R 語言中級函式程式設計

測試是否包含

x 在 y 裡嗎(%in%)?

status <- 201

good_status <- c(200, 201, 202, 203)

status %in% good_status
TRUE
使用 purrr 的 R 語言中級函式程式設計

一起來練習吧!

使用 purrr 的 R 語言中級函式程式設計

Preparing Video For Download...