forked from tidyverse/dplyr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstarwars.R
66 lines (51 loc) · 2.12 KB
/
starwars.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
library(tidyverse)
library(httr)
get_all <- function(url) {
out <- NULL
while (!is.null(url)) {
message("Getting ", url)
req <- GET(url)
stop_for_status(req)
con <- content(req)
out <- c(out, con$results)
url <- con$`next`
}
out
}
people <- get_all("http://swapi.co/api/people")
str(people[[1]])
# Generate lookup tables --------------------------------------------------
lookup <- function(url, name = "name") {
all <- get_all(url)
url <- all %>% map_chr("url")
name <- all %>% map_chr(name)
name[name == "unknown"] <- NA
set_names(name, url)
}
species <- lookup("http://swapi.co/api/species")
films <- lookup("http://swapi.co/api/films", "title")
planets <- lookup("http://swapi.co/api/planets")
vehicles <- lookup("http://swapi.co/api/vehicles")
starships <- lookup("http://swapi.co/api/starships")
starwars <- tibble(
name = people %>% map_chr("name"),
height = people %>% map_chr("height") %>% parse_integer(na = "unknown"),
mass = people %>% map_chr("mass") %>% parse_number(na = "unknown"),
hair_color = people %>% map_chr("hair_color") %>% parse_character(na = "n/a"),
skin_color = people %>% map_chr("skin_color"),
eye_color = people %>% map_chr("eye_color"),
birth_year = people %>% map_chr("birth_year") %>% parse_number(na = "unknown"),
gender = people %>% map_chr("gender") %>% parse_character(na = "n/a"),
homeworld = people %>% map_chr("homeworld") %>% planets[.] %>% unname(),
species = people %>% map("species") %>% map_chr(1, .null = NA) %>% species[.] %>% unname(),
films = people %>% map("films") %>% map(. %>% flatten_chr() %>% films[.] %>% unname()),
vehicles = people %>% map("vehicles") %>% map(. %>% flatten_chr() %>% vehicles[.] %>% unname()),
starships = people %>% map("starships") %>% map(. %>% flatten_chr() %>% starships[.] %>% unname())
)
# A little exploration
starwars %>% count(species, sort = TRUE)
starwars %>% count(homeworld, sort = TRUE)
starwars %>% count(skin_color, sort = TRUE)
starwars %>% count(gender, sort = TRUE)
starwars %>% group_by(species) %>% summarise(mass = mean(mass, na.rm = T))
devtools::use_data(starwars, overwrite = TRUE)