探索資料集

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

Colin Fay

Data Scientist & R Hacker at ThinkR

資料集

rstudioconf:包含 5055 則推文的清單

length(rstudioconf)

length(rstudioconf[[1]])
5055

31
library(purrr)
vec_depth(rstudioconf)
4

來源:ThinkR-open/datasets

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

JSON:典型的 API 輸出

JSON == 巢狀清單

{
    "menu": {
        "id": "file",
        "value": "File",
        "popup": {
            "menuitem": [
                { "value": "New", "onclick": "CreateNewDoc" },
                { "value": "Open", "onclick": "OpenDoc" },
                { "value": "Close", "onclick": "CloseDoc" }
            ]
        }
    }
}
使用 purrr 的 R 語言中級函式程式設計

述詞複習

我們已經看到:

  • map_*()

  • discard()

  • keep()

述詞函式

  • 接收元素與述詞
  • 對元素套用述詞
使用 purrr 的 R 語言中級函式程式設計

keep() 與 discard()

keep() 保留符合條件的元素:

keep(1:10, ~ .x < 5)
1 2 3 4

discard() 丟棄符合條件的元素:

discard(1:10, ~ .x < 5)
5  6  7  8  9 10
使用 purrr 的 R 語言中級函式程式設計

一起來練習吧!

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

Preparing Video For Download...