-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathanswers.Rmd
58 lines (41 loc) · 1.3 KB
/
answers.Rmd
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
---
title: "Using R to Orchestrate APIs -- Answers"
subtitle: "using An API Of Ice And Fire"
author: "John Little"
date: '`r Sys.Date()`'
output:
html_notebook: default
---
```{r load-libraries}
library(jsonlite)
```
1. What is the title of book10?
2. How many pages in book10?
3. How many characters in book 10?
```{r book10}
book10 <- fromJSON("http://anapioficeandfire.com/api/books/10")
book10$name # title
book10$numberOfPages # pages in book 10
length(book10$characters) # Number of characters in book 10
```
## Bonus Round
1. How many different publishers are there?
2. How many books did each publisher publish?
```{r bonus1}
library(tidyverse) # because I like working with modern data frames and tidy data
allBooks <- fromJSON("http://anapioficeandfire.com/api/books")
allBooks %>%
tbl_df() %>% # convert to tibble
group_by(publisher) %>%
summarise(TitlesPublished = length(publisher))
```
---
3. List each book with its publisher?
```{r bonus2}
paste(allBooks$name, allBooks$publisher,sep = " --- ") # List each book with its publisher
```
---
4. What is the total number of pages of all books in the series?
```{r bonus3}
sum(allBooks$numberOfPages) # Total number of pages of all books
```