Skip to content

Commit b2214be

Browse files
committed
update to use repurrrsive for data
1 parent 76c0c28 commit b2214be

File tree

7 files changed

+119
-85
lines changed

7 files changed

+119
-85
lines changed

01-map.R

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,26 @@
11
library(purrr)
22

3-
# loads objects: films, people, vehicles, starships,
4-
# planets & species
5-
load("data/swapi.rda")
3+
library(repurrrsive)
4+
# includes objects: sw_films, sw_people, sw_vehicles,
5+
# sw_starships, sw_planets & sw_species
66

7-
# How many elements are in people?
8-
length(people)
7+
# How many elements are in sw_people?
8+
length(sw_people)
99

10-
# Who is the first person in people?
11-
people[[1]] # Luke!
10+
# Who is the first person in sw_people?
11+
sw_people[[1]] # Luke!
1212

1313
# A list inside a list
14-
people[1]
14+
sw_people[1]
1515

1616
# A list, dropped one level of heirachy
17-
people[[1]]
17+
sw_people[[1]]
1818

1919
# How many starships has each character been on?
20-
map(people, ~ length(.x$starships))
20+
map(sw_people, ~ length(.x$starships))
2121

2222
# For later
23-
planet_lookup <- map_chr(planets, "name") %>% set_names(map_chr(planets, "url"))
23+
planet_lookup <- map_chr(sw_planets, "name") %>%
24+
set_names(map_chr(sw_planets, "url"))
2425
planet_lookup
2526
save(planet_lookup, file = "data/planet_lookup.rda", compress = FALSE)

02-typed-functions.R

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,44 @@
11
library(purrr)
2-
load("data/swapi.rda")
2+
library(repurrrsive)
33

44
# Typed functions
5-
people <- people %>% set_names(map_chr(people, "name"))
5+
sw_people <- sw_people %>% set_names(map_chr(sw_people, "name"))
66

77
# How many starships has each character been in?
8-
map(people, ~ length(.x[["starships"]]))
8+
map(sw_people, ~ length(.x[["starships"]]))
99

1010
# What color is each characters hair?
11-
map(people, ~ .x[["hair_color"]])
11+
map(sw_people, ~ .x[["hair_color"]])
1212

1313
# Is the character male?
14-
map(people, ~ .x[["gender"]] == "male")
14+
map(sw_people, ~ .x[["gender"]] == "male")
1515

1616
# How heavy is each character?
17-
map(people, ~ .x[["mass"]])
17+
map(sw_people, ~ .x[["mass"]])
1818

1919

20+
21+
22+
# Solutions ---------------------------------------------------------------
23+
2024
# How many starships has each character been in?
21-
map_int(people, ~ length(.x[["starships"]]))
25+
map_int(sw_people, ~ length(.x[["starships"]]))
2226

2327
# What color is each characters hair?
24-
map_chr(people, ~ .x[["hair_color"]])
28+
map_chr(sw_people, ~ .x[["hair_color"]])
2529

2630
# Is the character male?
27-
map_lgl(people, ~ .x[["gender"]] == "male")
31+
map_lgl(sw_people, ~ .x[["gender"]] == "male")
2832

2933
# How heavy is each character?
30-
map_dbl(people, ~ .x[["mass"]])
34+
map_dbl(sw_people, ~ .x[["mass"]])
3135
# Doesn't work...because we get a string back
32-
map(people, ~ .x[["mass"]])
36+
map(sw_people, ~ .x[["mass"]])
3337

3438
# A little risky
35-
map_dbl(people, ~ as.numeric(.x[["mass"]]))
39+
map_dbl(sw_people, ~ as.numeric(.x[["mass"]]))
3640

3741
# Probably want something like:
38-
map_chr(people, ~ .x[["mass"]]) %>%
42+
map_chr(sw_people, ~ .x[["mass"]]) %>%
3943
readr::parse_number(na = "unknown")
4044

03-challenges.R

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,29 @@
11
# Star wars challenges
22
library(purrr)
3-
4-
# loads objects: films, people, vehicles, starships,
5-
# planets & species
6-
load("data/swapi.rda")
3+
library(repurrrsive)
74

85
# Which film (see films) has the most characters?
9-
map(films, "characters") %>%
6+
map(sw_films, "characters") %>%
107
map_int(length) %>%
11-
set_names(map_chr(films, "title")) %>%
8+
set_names(map_chr(sw_films, "title")) %>%
129
sort()
1310

14-
# Create the planet_lookup vector from earlier.
15-
planet_lookup <- map_chr(planets, "name") %>%
16-
set_names(map(planets, "url"))
17-
1811
# Which species has the most possible eye colors?
19-
species[[1]]$eye_colors
12+
sw_species[[1]]$eye_colors
2013

21-
map_chr(species, "eye_colors") %>%
14+
map_chr(sw_species, "eye_colors") %>%
2215
strsplit(", ") %>%
23-
map_int(length)
16+
map_int(length) %>%
17+
set_names(map_chr(sw_species, "name"))
2418
# this is lazy, what about n/a and unknown?
2519

20+
# Which planets do we know the least about?
21+
# For one, entry 61
22+
map_lgl(sw_planets[[61]], ~ "unknown" %in% .x) %>%
23+
sum()
24+
25+
# For all
26+
map_int(sw_planets,
27+
~ map_lgl(.x, ~ "unknown" %in% .x) %>% sum()) %>%
28+
set_names(map_chr(sw_planets, "name")) %>%
29+
sort(decreasing = TRUE)

04-purrr-list-columns.R

Lines changed: 0 additions & 42 deletions
This file was deleted.

05-purrr-list-columns.R

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# star wars people list to data frame
2+
library(tidyverse)
3+
library(repurrrsive)
4+
5+
# A useful lookup table -----------------------------------------------
6+
film_number_lookup <- map_chr(sw_films, "url") %>%
7+
map(~ stringr::str_split_fixed(.x, "/", 7)[, 6]) %>%
8+
as.numeric() %>%
9+
set_names(map_chr(sw_films, "url"))
10+
11+
# Putting a few vars in a tibble --------------------------------------
12+
people_tbl <- tibble(
13+
name = sw_people %>% map_chr("name"),
14+
films = sw_people %>% map("films"),
15+
height = sw_people %>% map_chr("height") %>%
16+
readr::parse_number(na = "unknown"),
17+
species = sw_people %>% map_chr("species", .null = NA_character_)
18+
)
19+
20+
# Turning parts of our list to a tibble -----------------------------------
21+
people_tbl$films
22+
23+
# Use map with mutate to manipulate list columns
24+
people_tbl <- people_tbl %>%
25+
mutate(
26+
film_numbers = map(films,
27+
~ film_number_lookup[.x]),
28+
n_films = map_int(films, length)
29+
)
30+
31+
people_tbl %>% select(name, film_numbers, n_films)
32+
33+
34+
# Your Turn ---------------------------------------------------------------
35+
36+
# Create a new character column that collapses the film numbers in a single string,
37+
# e.g. for Luke " 6, 3, 2, 1, 7"
38+
39+
people_tbl <- people_tbl %>%
40+
mutate(films_squashed = map_chr(film_numbers, paste,
41+
collapse = ", "))
42+
43+
people_tbl %>% select(name, n_films, films_squashed)
44+

README.md

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# A purrr tutorial
22

3-
This repo hosts the materials for a [purrr](http://purrr.tidyverse.org/) tutorial.
3+
This repo hosts the materials for a [purrr](http://purrr.tidyverse.org/) tutorial. The materials currently reflect the version planned for the Cascadia R Conf, Portland, OR Jun 2017.
44

55
## Upcoming in-person tutorials
66

@@ -13,16 +13,39 @@ Older versions of the materials, from prior in-person tutorials, are also availa
1313
* [rstudio::conf Jan 2017 (2.25 hours)](https://github.com/cwickham/purrr-tutorial/tree/v0.1)
1414

1515

16-
17-
18-
## Abstract
19-
20-
Happy R users purrr: using functional programming to solve iteration problems
16+
## Outline
2117

2218
Code with a lot of duplication is harder to understand, troubleshoot and maintain. The goal of this tutorial is help you remove duplication in your code by using functions that write `for` loops for you.
2319

2420
You'll learn to use the functions in the `purrr` package to perform iterative tasks: tasks that look like "for each _____ do _____".
2521

2622
By the end of the tutorial you'll be writing code that is more readable and easier to update and you'll be ready to solve new iteration problems faster and with fewer mistakes.
2723

24+
## Learning Objectives
25+
26+
By the end of the tutorial, you'll be able to:
27+
28+
* Move from solving a problem on a single element, to iterating that solution over many elements with `map()`.
29+
* Identify when to use the typed variants of `map()`: `map_lgl()`, `map_int()`, `walk()` etc.
30+
* Iterate over two arguments with `map2()`.
31+
* Leverage `purrr` to get list data into tibbles.
32+
* Use `purrr` to work with list columns in tibbles.
33+
34+
## Pre-requisites
35+
36+
Don't worry if you have never written a `for` loop, used `lapply()`, written your own function or heard of a `tibble`, this tutorial is designed to be accessible to beginners.
37+
38+
That said, you should be familiar with exploring and subsetting the basic data structures in R including lists and data frames.
39+
40+
This is a hands-on tutorial, you'll need your laptop with R installed, as well as a few packages:
41+
42+
```{r}
43+
install.packages("tidyverse")
44+
45+
# install.packages("devtools")
46+
devtools::install_github("jennybc/repurrrsive")
47+
```
48+
49+
## License
50+
2851
<a rel="license" href="http://creativecommons.org/licenses/by-nc/4.0/"><img alt="Creative Commons License" style="border-width:0" src="https://i.creativecommons.org/l/by-nc/4.0/80x15.png" /></a><br />This <span xmlns:dct="http://purl.org/dc/terms/" property="dct:title">purrr tutorial</span> by <a xmlns:cc="http://creativecommons.org/ns#" href="https://github.com/cwickham/purrr-tutorial" property="cc:attributionName" rel="cc:attributionURL">Charlotte Wickham</a> is licensed under a <a rel="license" href="http://creativecommons.org/licenses/by-nc/4.0/">Creative Commons Attribution-NonCommercial 4.0 International License</a>.

0 commit comments

Comments
 (0)