Discovering the dataset

Intermediate Functional Programming with purrr

Colin Fay

Data Scientist & R Hacker at ThinkR

The dataset

rstudioconf: a list of 5055 tweets

length(rstudioconf)

length(rstudioconf[[1]])
5055

31
library(purrr)
vec_depth(rstudioconf)
4

Source : ThinkR-open/datasets

Intermediate Functional Programming with purrr

JSON - A typical API output

JSON == nested lists

{
    "menu": {
        "id": "file",
        "value": "File",
        "popup": {
            "menuitem": [
                { "value": "New", "onclick": "CreateNewDoc" },
                { "value": "Open", "onclick": "OpenDoc" },
                { "value": "Close", "onclick": "CloseDoc" }
            ]
        }
    }
}
Intermediate Functional Programming with purrr

Predicate refresher

We've seen:

  • map_*()

  • discard()

  • keep()

Predicate functionals:

  • Take an element & a predicate
  • Use the predicate on the element
Intermediate Functional Programming with purrr

keep() & discard()

keep() the elements that meet a condition:

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

discard() the elements that meet a condition:

discard(1:10, ~ .x < 5)
5  6  7  8  9 10
Intermediate Functional Programming with purrr

Let's practice!

Intermediate Functional Programming with purrr

Preparing Video For Download...