+ - 0:00:00
Notes for current slide
Notes for next slide
These slides are viewed best by Chrome and occasionally need to be refreshed if elements did not load properly. See here for PDF .


Press the right arrow to progress to the next slide!

1/45


ETC1010: Introduction to Data Analysis

Week 6, part B


Functions

Lecturer: Nicholas Tierney

Department of Econometrics and Business Statistics

ETC1010.Clayton-x@monash.edu

April 2020


1/45

Recap

  • File Paths
  • functions
2/45

Assignment 1

  • You all did really well! I'm so proud.
  • Sherry marked the exams and had the following to say:
3/45

Assignment 1

  • Good:
    • People have successfully knitted the .rmd document into an html file that combines all the code, text and R outputs - This is a big achievement for people using R the first time!
    • Most people answer questions well
    • In answering the second last question, we do have 2-3 groups go beyond the question and analyse the extra spreadsheet using tidy verbs and produce some insights - well done those groups!
4/45

Assignment 1

  • To be improved:
    • Most groups forget to factorise the year in the n_incident vs. year plot. It doesn’t make sense to display a year label of "2017.5" in the plot.
  • Clarification:
    • In the question "Can you tell me what each row represents, and what each of the columns measure", I give marks to groups that answer "each row represents an observation, ...", but this is a sloppy answer from my view because in our dataset, it is really "each row represents an observation of the number of incidents" rather than "each row represents an observation of one incident". Those who answer the latter lose 0.5 marks.
5/45

Assignment 2

  • Currently under review from the tutors, this will be released by Friday
  • You will all get Two weeks from the release date - this will be updated in the assessment.
6/45

Motivating Functions

7/45

Remember web scraping?

8/45

How many episodes in Stranger Things?

st_episode <-
bow("https://www.imdb.com/title/tt4574334/") %>%
scrape() %>%
html_nodes(".np_right_arrow .bp_sub_heading") %>%
html_text() %>%
str_remove(" episodes") %>%
as.numeric()
st_episode
## [1] 33
9/45

How many episodes in Stranger Things? And Mindhunter?

st_episode <- bow("https://www.imdb.com/title/tt4574334/") %>%
scrape() %>%
html_nodes(".np_right_arrow .bp_sub_heading") %>%
html_text() %>%
str_remove(" episodes") %>%
as.numeric()
st_episode
## [1] 33
mh_episodes <- bow("https://www.imdb.com/title/tt4574334/") %>%
scrape() %>%
html_nodes(".np_right_arrow .bp_sub_heading") %>%
html_text() %>%
str_remove(" episodes") %>%
as.numeric()
mh_episodes
## [1] 33
10/45

Why functions?

  • Automate common tasks in a power powerful and general way than copy-and-pasting:
    • Give a functions an evocative name that makes code easier to understand.
    • As requirements change, you only need to update code in one place, instead of many.
    • You eliminate the chance of making incidental mistakes when you copy and paste (i.e. updating a variable name in one place, but not in another).
11/45

Why functions?

  • Down the line: Improve your reach as a data scientist by writing functions (and packages!) that others use
12/45

Setup

library(tidyverse)
library(rvest)
library(polite)
st <- bow("http://www.imdb.com/title/tt4574334/") %>% scrape()
twd <- bow("http://www.imdb.com/title/tt1520211/") %>% scrape()
got <- bow("http://www.imdb.com/title/tt0944947/") %>% scrape()
13/45

When should you write a function?

Whenever you’ve copied and pasted a block of code more than twice.

When you want to clearly express some set of actions

(there are many other reasons as well!)

14/45

Do you see any problems in the code below?

st_episode <- st %>%
html_nodes(".np_right_arrow .bp_sub_heading") %>%
html_text() %>%
str_replace(" episodes", "") %>%
as.numeric()
got_episode <- got %>%
html_nodes(".np_right_arrow .bp_sub_heading") %>%
html_text() %>%
str_replace(" episodes", "") %>%
as.numeric()
twd_episode <- got %>%
html_nodes(".np_right_arrow .bp_sub_heading") %>%
html_text() %>%
str_replace(" episodes", "") %>%
as.numeric()
15/45

Inputs

How many inputs does the following code have?

st_episode <- st %>%
html_nodes(".np_right_arrow .bp_sub_heading") %>%
html_text() %>%
str_replace(" episodes", "") %>%
as.numeric()
16/45

Turn the code into a function

Pick a short but informative name, preferably a verb, but not always





episodes <-
17/45

Turn your code into a function

  • Pick a short but informative name, preferably a verb.
  • List inputs, or arguments, to the function inside function. If we had more the call would look like function(x, y, z).


episodes <- function(x){
}
18/45

Turn your code into a function

  • Pick a short but informative name, preferably a verb.
  • List inputs, or arguments, to the function inside function. If we had more the call would look like function(x, y, z).
  • Place the code you have developed in body of the function, a { block that immediately follows function(...).
episodes <- function(x){
x %>%
html_nodes(".np_right_arrow .bp_sub_heading") %>%
html_text() %>%
str_replace(" episodes", "") %>%
as.numeric()
}
19/45

Turn your code into a function

episodes <- function(x){
x %>%
html_nodes(".np_right_arrow .bp_sub_heading") %>%
html_text() %>%
str_replace(" episodes", "") %>%
as.numeric()
}
episodes(st)
## [1] 33
20/45

Check your function

  • Number of episodes in The Walking Dead
episodes(twd)
## [1] 148
  • Number of episodes in Game of Thrones
episodes(got)
## [1] 73
21/45

Naming functions (it's hard)

"There are only two hard things in Computer Science: cache invalidation and naming things." - Phil Karlton

22/45

Naming functions (it's hard)

"There are only two hard things in Computer Science: cache invalidation and naming things." - Phil Karlton

  • Names should be short but clearly evoke what the function does
22/45

Naming functions (it's hard)

"There are only two hard things in Computer Science: cache invalidation and naming things." - Phil Karlton

  • Names should be short but clearly evoke what the function does

  • Names should be verbs, not nouns

22/45

Naming functions (it's hard)

"There are only two hard things in Computer Science: cache invalidation and naming things." - Phil Karlton

  • Names should be short but clearly evoke what the function does

  • Names should be verbs, not nouns

  • Multi-word names should be separated by underscores (snake_case as opposed to camelCase)

22/45

Naming functions (it's hard)

"There are only two hard things in Computer Science: cache invalidation and naming things." - Phil Karlton

  • Names should be short but clearly evoke what the function does

  • Names should be verbs, not nouns

  • Multi-word names should be separated by underscores (snake_case as opposed to camelCase)

  • A family of functions should be named similarly (title, episodes, genre, etc.)

22/45

Naming functions (it's hard)

"There are only two hard things in Computer Science: cache invalidation and naming things." - Phil Karlton

  • Names should be short but clearly evoke what the function does

  • Names should be verbs, not nouns

  • Multi-word names should be separated by underscores (snake_case as opposed to camelCase)

  • A family of functions should be named similarly (title, episodes, genre, etc.)

  • Avoid overwriting existing (especially widely used) functions (e.g., ggplot)

22/45

Scraping show info

scrape_show_info <- function(x){
title <- x %>%
html_node("#title-overview-widget h1") %>%
html_text() %>%
str_trim()
runtime <- x %>%
html_node("time") %>%
html_text() %>%
str_replace("\\n", "") %>%
str_trim()
genres <- x %>%
html_nodes(".txt-block~ .canwrap a") %>%
html_text() %>%
str_trim() %>%
paste(collapse = ", ")
tibble(title = title, runtime = runtime, genres = genres)
}
23/45

Scraping show info

scrape_show_info(st)
## # A tibble: 1 x 3
## title runtime genres
## <chr> <chr> <chr>
## 1 Stranger Things 51min Drama, Fantasy, Horror, Mystery, Sci-Fi, Thriller
scrape_show_info(twd)
## # A tibble: 1 x 3
## title runtime genres
## <chr> <chr> <chr>
## 1 The Walking Dead 44min Drama, Horror, Thriller
24/45

How to update this function to use page URL as argument?

scrape_show_info <- function(x){
title <- x %>% html_node("#title-overview-widget h1") %>%
html_text() %>%
str_trim()
runtime <- x %>% html_node("time") %>%
html_text() %>%
str_replace("\\n", "") %>%
str_trim()
genres <- x %>% html_nodes(".txt-block~ .canwrap a") %>%
html_text() %>%
str_trim() %>%
paste(collapse = ", ")
tibble(title = title, runtime = runtime, genres = genres)
}
25/45

How to update this function to use page URL as argument?

scrape_show_info <- function(x){
y <- bow(x) %>% scrape()
title <- y %>% html_node("#title-overview-widget h1") %>%
html_text() %>%
str_trim()
runtime <- y %>% html_node("time") %>%
html_text() %>%
str_replace("\\n", "") %>%
str_trim()
genres <- y %>% html_nodes(".txt-block~ .canwrap a") %>%
html_text() %>%
str_trim() %>%
paste(collapse = ", ")
tibble(title = title, runtime = runtime, genres = genres)
}
26/45

Let's check

st_url <- "http://www.imdb.com/title/tt4574334/"
twd_url <- "http://www.imdb.com/title/tt1520211/"
27/45

Let's check

st_url <- "http://www.imdb.com/title/tt4574334/"
twd_url <- "http://www.imdb.com/title/tt1520211/"
scrape_show_info(st_url)
## # A tibble: 1 x 3
## title runtime genres
## <chr> <chr> <chr>
## 1 Stranger Things 51min Drama, Fantasy, Horror, Mystery, Sci-Fi, Thriller
scrape_show_info(twd_url)
## # A tibble: 1 x 3
## title runtime genres
## <chr> <chr> <chr>
## 1 The Walking Dead 44min Drama, Horror, Thriller
27/45

Automation

28/45

Automation

  • You now have a function that will scrape the relevant info on shows given its URL.
  • Where can we get a list of URLs of top 100 most popular TV shows on IMDB?
  • Write the code for doing this in your teams.
29/45

Automation

urls <- bow("http://www.imdb.com/chart/tvmeter") %>%
scrape() %>%
html_nodes(".titleColumn a") %>%
html_attr("href") %>%
paste("http://www.imdb.com", ., sep = "")
## [1] "http://www.imdb.com/title/tt6468322/?pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=332cb927-0342-42b3-815c-f9124e84021d&pf_rd_r=ZYRJB8E5SN8XG4BRVG76&pf_rd_s=center-1&pf_rd_t=15506&pf_rd_i=tvmeter&ref_=chttvm_tt_1"
## [2] "http://www.imdb.com/title/tt5071412/?pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=332cb927-0342-42b3-815c-f9124e84021d&pf_rd_r=ZYRJB8E5SN8XG4BRVG76&pf_rd_s=center-1&pf_rd_t=15506&pf_rd_i=tvmeter&ref_=chttvm_tt_2"
## [3] "http://www.imdb.com/title/tt3032476/?pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=332cb927-0342-42b3-815c-f9124e84021d&pf_rd_r=ZYRJB8E5SN8XG4BRVG76&pf_rd_s=center-1&pf_rd_t=15506&pf_rd_i=tvmeter&ref_=chttvm_tt_3"
## [4] "http://www.imdb.com/title/tt10293938/?pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=332cb927-0342-42b3-815c-f9124e84021d&pf_rd_r=ZYRJB8E5SN8XG4BRVG76&pf_rd_s=center-1&pf_rd_t=15506&pf_rd_i=tvmeter&ref_=chttvm_tt_4"
## [5] "http://www.imdb.com/title/tt6040674/?pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=332cb927-0342-42b3-815c-f9124e84021d&pf_rd_r=ZYRJB8E5SN8XG4BRVG76&pf_rd_s=center-1&pf_rd_t=15506&pf_rd_i=tvmeter&ref_=chttvm_tt_5"
## [6] "http://www.imdb.com/title/tt0475784/?pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=332cb927-0342-42b3-815c-f9124e84021d&pf_rd_r=ZYRJB8E5SN8XG4BRVG76&pf_rd_s=center-1&pf_rd_t=15506&pf_rd_i=tvmeter&ref_=chttvm_tt_6"
## [7] "http://www.imdb.com/title/tt1439629/?pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=332cb927-0342-42b3-815c-f9124e84021d&pf_rd_r=ZYRJB8E5SN8XG4BRVG76&pf_rd_s=center-1&pf_rd_t=15506&pf_rd_i=tvmeter&ref_=chttvm_tt_7"
## [8] "http://www.imdb.com/title/tt12004280/?pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=332cb927-0342-42b3-815c-f9124e84021d&pf_rd_r=ZYRJB8E5SN8XG4BRVG76&pf_rd_s=center-1&pf_rd_t=15506&pf_rd_i=tvmeter&ref_=chttvm_tt_8"
## [9] "http://www.imdb.com/title/tt3502248/?pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=332cb927-0342-42b3-815c-f9124e84021d&pf_rd_r=ZYRJB8E5SN8XG4BRVG76&pf_rd_s=center-1&pf_rd_t=15506&pf_rd_i=tvmeter&ref_=chttvm_tt_9"
## [10] "http://www.imdb.com/title/tt0944947/?pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=332cb927-0342-42b3-815c-f9124e84021d&pf_rd_r=ZYRJB8E5SN8XG4BRVG76&pf_rd_s=center-1&pf_rd_t=15506&pf_rd_i=tvmeter&ref_=chttvm_tt_10"
## [11] "http://www.imdb.com/title/tt0903747/?pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=332cb927-0342-42b3-815c-f9124e84021d&pf_rd_r=ZYRJB8E5SN8XG4BRVG76&pf_rd_s=center-1&pf_rd_t=15506&pf_rd_i=tvmeter&ref_=chttvm_tt_11"
## [12] "http://www.imdb.com/title/tt1520211/?pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=332cb927-0342-42b3-815c-f9124e84021d&pf_rd_r=ZYRJB8E5SN8XG4BRVG76&pf_rd_s=center-1&pf_rd_t=15506&pf_rd_i=tvmeter&ref_=chttvm_tt_12"
## [13] "http://www.imdb.com/title/tt1796960/?pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=332cb927-0342-42b3-815c-f9124e84021d&pf_rd_r=ZYRJB8E5SN8XG4BRVG76&pf_rd_s=center-1&pf_rd_t=15506&pf_rd_i=tvmeter&ref_=chttvm_tt_13"
## [14] "http://www.imdb.com/title/tt2442560/?pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=332cb927-0342-42b3-815c-f9124e84021d&pf_rd_r=ZYRJB8E5SN8XG4BRVG76&pf_rd_s=center-1&pf_rd_t=15506&pf_rd_i=tvmeter&ref_=chttvm_tt_14"
## [15] "http://www.imdb.com/title/tt0386676/?pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=332cb927-0342-42b3-815c-f9124e84021d&pf_rd_r=ZYRJB8E5SN8XG4BRVG76&pf_rd_s=center-1&pf_rd_t=15506&pf_rd_i=tvmeter&ref_=chttvm_tt_15"
## [16] "http://www.imdb.com/title/tt9827854/?pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=332cb927-0342-42b3-815c-f9124e84021d&pf_rd_r=ZYRJB8E5SN8XG4BRVG76&pf_rd_s=center-1&pf_rd_t=15506&pf_rd_i=tvmeter&ref_=chttvm_tt_16"
## [17] "http://www.imdb.com/title/tt7016936/?pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=332cb927-0342-42b3-815c-f9124e84021d&pf_rd_r=ZYRJB8E5SN8XG4BRVG76&pf_rd_s=center-1&pf_rd_t=15506&pf_rd_i=tvmeter&ref_=chttvm_tt_17"
## [18] "http://www.imdb.com/title/tt8111088/?pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=332cb927-0342-42b3-815c-f9124e84021d&pf_rd_r=ZYRJB8E5SN8XG4BRVG76&pf_rd_s=center-1&pf_rd_t=15506&pf_rd_i=tvmeter&ref_=chttvm_tt_18"
## [19] "http://www.imdb.com/title/tt8089592/?pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=332cb927-0342-42b3-815c-f9124e84021d&pf_rd_r=ZYRJB8E5SN8XG4BRVG76&pf_rd_s=center-1&pf_rd_t=15506&pf_rd_i=tvmeter&ref_=chttvm_tt_19"
## [20] "http://www.imdb.com/title/tt9815454/?pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=332cb927-0342-42b3-815c-f9124e84021d&pf_rd_r=ZYRJB8E5SN8XG4BRVG76&pf_rd_s=center-1&pf_rd_t=15506&pf_rd_i=tvmeter&ref_=chttvm_tt_20"
## [21] "http://www.imdb.com/title/tt0413573/?pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=332cb927-0342-42b3-815c-f9124e84021d&pf_rd_r=ZYRJB8E5SN8XG4BRVG76&pf_rd_s=center-1&pf_rd_t=15506&pf_rd_i=tvmeter&ref_=chttvm_tt_21"
## [22] "http://www.imdb.com/title/tt3006802/?pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=332cb927-0342-42b3-815c-f9124e84021d&pf_rd_r=ZYRJB8E5SN8XG4BRVG76&pf_rd_s=center-1&pf_rd_t=15506&pf_rd_i=tvmeter&ref_=chttvm_tt_22"
## [23] "http://www.imdb.com/title/tt2467372/?pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=332cb927-0342-42b3-815c-f9124e84021d&pf_rd_r=ZYRJB8E5SN8XG4BRVG76&pf_rd_s=center-1&pf_rd_t=15506&pf_rd_i=tvmeter&ref_=chttvm_tt_23"
## [24] "http://www.imdb.com/title/tt0458290/?pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=332cb927-0342-42b3-815c-f9124e84021d&pf_rd_r=ZYRJB8E5SN8XG4BRVG76&pf_rd_s=center-1&pf_rd_t=15506&pf_rd_i=tvmeter&ref_=chttvm_tt_24"
## [25] "http://www.imdb.com/title/tt2306299/?pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=332cb927-0342-42b3-815c-f9124e84021d&pf_rd_r=ZYRJB8E5SN8XG4BRVG76&pf_rd_s=center-1&pf_rd_t=15506&pf_rd_i=tvmeter&ref_=chttvm_tt_25"
## [26] "http://www.imdb.com/title/tt8134186/?pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=332cb927-0342-42b3-815c-f9124e84021d&pf_rd_r=ZYRJB8E5SN8XG4BRVG76&pf_rd_s=center-1&pf_rd_t=15506&pf_rd_i=tvmeter&ref_=chttvm_tt_26"
## [27] "http://www.imdb.com/title/tt8420184/?pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=332cb927-0342-42b3-815c-f9124e84021d&pf_rd_r=ZYRJB8E5SN8XG4BRVG76&pf_rd_s=center-1&pf_rd_t=15506&pf_rd_i=tvmeter&ref_=chttvm_tt_27"
## [28] "http://www.imdb.com/title/tt4574334/?pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=332cb927-0342-42b3-815c-f9124e84021d&pf_rd_r=ZYRJB8E5SN8XG4BRVG76&pf_rd_s=center-1&pf_rd_t=15506&pf_rd_i=tvmeter&ref_=chttvm_tt_28"
## [29] "http://www.imdb.com/title/tt10228230/?pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=332cb927-0342-42b3-815c-f9124e84021d&pf_rd_r=ZYRJB8E5SN8XG4BRVG76&pf_rd_s=center-1&pf_rd_t=15506&pf_rd_i=tvmeter&ref_=chttvm_tt_29"
## [30] "http://www.imdb.com/title/tt10311562/?pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=332cb927-0342-42b3-815c-f9124e84021d&pf_rd_r=ZYRJB8E5SN8XG4BRVG76&pf_rd_s=center-1&pf_rd_t=15506&pf_rd_i=tvmeter&ref_=chttvm_tt_30"
## [31] "http://www.imdb.com/title/tt11823076/?pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=332cb927-0342-42b3-815c-f9124e84021d&pf_rd_r=ZYRJB8E5SN8XG4BRVG76&pf_rd_s=center-1&pf_rd_t=15506&pf_rd_i=tvmeter&ref_=chttvm_tt_31"
## [32] "http://www.imdb.com/title/tt11639414/?pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=332cb927-0342-42b3-815c-f9124e84021d&pf_rd_r=ZYRJB8E5SN8XG4BRVG76&pf_rd_s=center-1&pf_rd_t=15506&pf_rd_i=tvmeter&ref_=chttvm_tt_32"
## [33] "http://www.imdb.com/title/tt3526078/?pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=332cb927-0342-42b3-815c-f9124e84021d&pf_rd_r=ZYRJB8E5SN8XG4BRVG76&pf_rd_s=center-1&pf_rd_t=15506&pf_rd_i=tvmeter&ref_=chttvm_tt_33"
## [34] "http://www.imdb.com/title/tt0460681/?pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=332cb927-0342-42b3-815c-f9124e84021d&pf_rd_r=ZYRJB8E5SN8XG4BRVG76&pf_rd_s=center-1&pf_rd_t=15506&pf_rd_i=tvmeter&ref_=chttvm_tt_34"
## [35] "http://www.imdb.com/title/tt2741602/?pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=332cb927-0342-42b3-815c-f9124e84021d&pf_rd_r=ZYRJB8E5SN8XG4BRVG76&pf_rd_s=center-1&pf_rd_t=15506&pf_rd_i=tvmeter&ref_=chttvm_tt_35"
## [36] "http://www.imdb.com/title/tt4565380/?pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=332cb927-0342-42b3-815c-f9124e84021d&pf_rd_r=ZYRJB8E5SN8XG4BRVG76&pf_rd_s=center-1&pf_rd_t=15506&pf_rd_i=tvmeter&ref_=chttvm_tt_36"
## [37] "http://www.imdb.com/title/tt8398600/?pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=332cb927-0342-42b3-815c-f9124e84021d&pf_rd_r=ZYRJB8E5SN8XG4BRVG76&pf_rd_s=center-1&pf_rd_t=15506&pf_rd_i=tvmeter&ref_=chttvm_tt_37"
## [38] "http://www.imdb.com/title/tt9244556/?pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=332cb927-0342-42b3-815c-f9124e84021d&pf_rd_r=ZYRJB8E5SN8XG4BRVG76&pf_rd_s=center-1&pf_rd_t=15506&pf_rd_i=tvmeter&ref_=chttvm_tt_38"
## [39] "http://www.imdb.com/title/tt0108778/?pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=332cb927-0342-42b3-815c-f9124e84021d&pf_rd_r=ZYRJB8E5SN8XG4BRVG76&pf_rd_s=center-1&pf_rd_t=15506&pf_rd_i=tvmeter&ref_=chttvm_tt_39"
## [40] "http://www.imdb.com/title/tt4179452/?pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=332cb927-0342-42b3-815c-f9124e84021d&pf_rd_r=ZYRJB8E5SN8XG4BRVG76&pf_rd_s=center-1&pf_rd_t=15506&pf_rd_i=tvmeter&ref_=chttvm_tt_40"
## [41] "http://www.imdb.com/title/tt0306414/?pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=332cb927-0342-42b3-815c-f9124e84021d&pf_rd_r=ZYRJB8E5SN8XG4BRVG76&pf_rd_s=center-1&pf_rd_t=15506&pf_rd_i=tvmeter&ref_=chttvm_tt_41"
## [42] "http://www.imdb.com/title/tt0141842/?pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=332cb927-0342-42b3-815c-f9124e84021d&pf_rd_r=ZYRJB8E5SN8XG4BRVG76&pf_rd_s=center-1&pf_rd_t=15506&pf_rd_i=tvmeter&ref_=chttvm_tt_42"
## [43] "http://www.imdb.com/title/tt0452046/?pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=332cb927-0342-42b3-815c-f9124e84021d&pf_rd_r=ZYRJB8E5SN8XG4BRVG76&pf_rd_s=center-1&pf_rd_t=15506&pf_rd_i=tvmeter&ref_=chttvm_tt_43"
## [44] "http://www.imdb.com/title/tt5753856/?pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=332cb927-0342-42b3-815c-f9124e84021d&pf_rd_r=ZYRJB8E5SN8XG4BRVG76&pf_rd_s=center-1&pf_rd_t=15506&pf_rd_i=tvmeter&ref_=chttvm_tt_44"
## [45] "http://www.imdb.com/title/tt10098482/?pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=332cb927-0342-42b3-815c-f9124e84021d&pf_rd_r=ZYRJB8E5SN8XG4BRVG76&pf_rd_s=center-1&pf_rd_t=15506&pf_rd_i=tvmeter&ref_=chttvm_tt_45"
## [46] "http://www.imdb.com/title/tt7661390/?pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=332cb927-0342-42b3-815c-f9124e84021d&pf_rd_r=ZYRJB8E5SN8XG4BRVG76&pf_rd_s=center-1&pf_rd_t=15506&pf_rd_i=tvmeter&ref_=chttvm_tt_46"
## [47] "http://www.imdb.com/title/tt4052886/?pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=332cb927-0342-42b3-815c-f9124e84021d&pf_rd_r=ZYRJB8E5SN8XG4BRVG76&pf_rd_s=center-1&pf_rd_t=15506&pf_rd_i=tvmeter&ref_=chttvm_tt_47"
## [48] "http://www.imdb.com/title/tt0455275/?pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=332cb927-0342-42b3-815c-f9124e84021d&pf_rd_r=ZYRJB8E5SN8XG4BRVG76&pf_rd_s=center-1&pf_rd_t=15506&pf_rd_i=tvmeter&ref_=chttvm_tt_48"
## [49] "http://www.imdb.com/title/tt2085059/?pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=332cb927-0342-42b3-815c-f9124e84021d&pf_rd_r=ZYRJB8E5SN8XG4BRVG76&pf_rd_s=center-1&pf_rd_t=15506&pf_rd_i=tvmeter&ref_=chttvm_tt_49"
## [50] "http://www.imdb.com/title/tt3107288/?pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=332cb927-0342-42b3-815c-f9124e84021d&pf_rd_r=ZYRJB8E5SN8XG4BRVG76&pf_rd_s=center-1&pf_rd_t=15506&pf_rd_i=tvmeter&ref_=chttvm_tt_50"
## [51] "http://www.imdb.com/title/tt0203259/?pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=332cb927-0342-42b3-815c-f9124e84021d&pf_rd_r=ZYRJB8E5SN8XG4BRVG76&pf_rd_s=center-1&pf_rd_t=15506&pf_rd_i=tvmeter&ref_=chttvm_tt_51"
## [52] "http://www.imdb.com/title/tt7908628/?pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=332cb927-0342-42b3-815c-f9124e84021d&pf_rd_r=ZYRJB8E5SN8XG4BRVG76&pf_rd_s=center-1&pf_rd_t=15506&pf_rd_i=tvmeter&ref_=chttvm_tt_52"
## [53] "http://www.imdb.com/title/tt2802850/?pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=332cb927-0342-42b3-815c-f9124e84021d&pf_rd_r=ZYRJB8E5SN8XG4BRVG76&pf_rd_s=center-1&pf_rd_t=15506&pf_rd_i=tvmeter&ref_=chttvm_tt_53"
## [54] "http://www.imdb.com/title/tt6874964/?pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=332cb927-0342-42b3-815c-f9124e84021d&pf_rd_r=ZYRJB8E5SN8XG4BRVG76&pf_rd_s=center-1&pf_rd_t=15506&pf_rd_i=tvmeter&ref_=chttvm_tt_54"
## [55] "http://www.imdb.com/title/tt5420376/?pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=332cb927-0342-42b3-815c-f9124e84021d&pf_rd_r=ZYRJB8E5SN8XG4BRVG76&pf_rd_s=center-1&pf_rd_t=15506&pf_rd_i=tvmeter&ref_=chttvm_tt_55"
## [56] "http://www.imdb.com/title/tt5180504/?pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=332cb927-0342-42b3-815c-f9124e84021d&pf_rd_r=ZYRJB8E5SN8XG4BRVG76&pf_rd_s=center-1&pf_rd_t=15506&pf_rd_i=tvmeter&ref_=chttvm_tt_56"
## [57] "http://www.imdb.com/title/tt8741290/?pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=332cb927-0342-42b3-815c-f9124e84021d&pf_rd_r=ZYRJB8E5SN8XG4BRVG76&pf_rd_s=center-1&pf_rd_t=15506&pf_rd_i=tvmeter&ref_=chttvm_tt_57"
## [58] "http://www.imdb.com/title/tt2304589/?pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=332cb927-0342-42b3-815c-f9124e84021d&pf_rd_r=ZYRJB8E5SN8XG4BRVG76&pf_rd_s=center-1&pf_rd_t=15506&pf_rd_i=tvmeter&ref_=chttvm_tt_58"
## [59] "http://www.imdb.com/title/tt0364845/?pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=332cb927-0342-42b3-815c-f9124e84021d&pf_rd_r=ZYRJB8E5SN8XG4BRVG76&pf_rd_s=center-1&pf_rd_t=15506&pf_rd_i=tvmeter&ref_=chttvm_tt_59"
## [60] "http://www.imdb.com/title/tt2661044/?pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=332cb927-0342-42b3-815c-f9124e84021d&pf_rd_r=ZYRJB8E5SN8XG4BRVG76&pf_rd_s=center-1&pf_rd_t=15506&pf_rd_i=tvmeter&ref_=chttvm_tt_60"
## [61] "http://www.imdb.com/title/tt9642982/?pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=332cb927-0342-42b3-815c-f9124e84021d&pf_rd_r=ZYRJB8E5SN8XG4BRVG76&pf_rd_s=center-1&pf_rd_t=15506&pf_rd_i=tvmeter&ref_=chttvm_tt_61"
## [62] "http://www.imdb.com/title/tt0804503/?pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=332cb927-0342-42b3-815c-f9124e84021d&pf_rd_r=ZYRJB8E5SN8XG4BRVG76&pf_rd_s=center-1&pf_rd_t=15506&pf_rd_i=tvmeter&ref_=chttvm_tt_62"
## [63] "http://www.imdb.com/title/tt5834204/?pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=332cb927-0342-42b3-815c-f9124e84021d&pf_rd_r=ZYRJB8E5SN8XG4BRVG76&pf_rd_s=center-1&pf_rd_t=15506&pf_rd_i=tvmeter&ref_=chttvm_tt_63"
## [64] "http://www.imdb.com/title/tt7366338/?pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=332cb927-0342-42b3-815c-f9124e84021d&pf_rd_r=ZYRJB8E5SN8XG4BRVG76&pf_rd_s=center-1&pf_rd_t=15506&pf_rd_i=tvmeter&ref_=chttvm_tt_64"
## [65] "http://www.imdb.com/title/tt7414406/?pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=332cb927-0342-42b3-815c-f9124e84021d&pf_rd_r=ZYRJB8E5SN8XG4BRVG76&pf_rd_s=center-1&pf_rd_t=15506&pf_rd_i=tvmeter&ref_=chttvm_tt_65"
## [66] "http://www.imdb.com/title/tt1405406/?pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=332cb927-0342-42b3-815c-f9124e84021d&pf_rd_r=ZYRJB8E5SN8XG4BRVG76&pf_rd_s=center-1&pf_rd_t=15506&pf_rd_i=tvmeter&ref_=chttvm_tt_66"
## [67] "http://www.imdb.com/title/tt0898266/?pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=332cb927-0342-42b3-815c-f9124e84021d&pf_rd_r=ZYRJB8E5SN8XG4BRVG76&pf_rd_s=center-1&pf_rd_t=15506&pf_rd_i=tvmeter&ref_=chttvm_tt_67"
## [68] "http://www.imdb.com/title/tt0411008/?pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=332cb927-0342-42b3-815c-f9124e84021d&pf_rd_r=ZYRJB8E5SN8XG4BRVG76&pf_rd_s=center-1&pf_rd_t=15506&pf_rd_i=tvmeter&ref_=chttvm_tt_68"
## [69] "http://www.imdb.com/title/tt1844624/?pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=332cb927-0342-42b3-815c-f9124e84021d&pf_rd_r=ZYRJB8E5SN8XG4BRVG76&pf_rd_s=center-1&pf_rd_t=15506&pf_rd_i=tvmeter&ref_=chttvm_tt_69"
## [70] "http://www.imdb.com/title/tt1586680/?pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=332cb927-0342-42b3-815c-f9124e84021d&pf_rd_r=ZYRJB8E5SN8XG4BRVG76&pf_rd_s=center-1&pf_rd_t=15506&pf_rd_i=tvmeter&ref_=chttvm_tt_70"
## [71] "http://www.imdb.com/title/tt3205802/?pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=332cb927-0342-42b3-815c-f9124e84021d&pf_rd_r=ZYRJB8E5SN8XG4BRVG76&pf_rd_s=center-1&pf_rd_t=15506&pf_rd_i=tvmeter&ref_=chttvm_tt_71"
## [72] "http://www.imdb.com/title/tt1266020/?pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=332cb927-0342-42b3-815c-f9124e84021d&pf_rd_r=ZYRJB8E5SN8XG4BRVG76&pf_rd_s=center-1&pf_rd_t=15506&pf_rd_i=tvmeter&ref_=chttvm_tt_72"
## [73] "http://www.imdb.com/title/tt1632701/?pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=332cb927-0342-42b3-815c-f9124e84021d&pf_rd_r=ZYRJB8E5SN8XG4BRVG76&pf_rd_s=center-1&pf_rd_t=15506&pf_rd_i=tvmeter&ref_=chttvm_tt_73"
## [74] "http://www.imdb.com/title/tt1124373/?pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=332cb927-0342-42b3-815c-f9124e84021d&pf_rd_r=ZYRJB8E5SN8XG4BRVG76&pf_rd_s=center-1&pf_rd_t=15506&pf_rd_i=tvmeter&ref_=chttvm_tt_74"
## [75] "http://www.imdb.com/title/tt7767422/?pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=332cb927-0342-42b3-815c-f9124e84021d&pf_rd_r=ZYRJB8E5SN8XG4BRVG76&pf_rd_s=center-1&pf_rd_t=15506&pf_rd_i=tvmeter&ref_=chttvm_tt_75"
## [76] "http://www.imdb.com/title/tt2356777/?pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=332cb927-0342-42b3-815c-f9124e84021d&pf_rd_r=ZYRJB8E5SN8XG4BRVG76&pf_rd_s=center-1&pf_rd_t=15506&pf_rd_i=tvmeter&ref_=chttvm_tt_76"
## [77] "http://www.imdb.com/title/tt7134908/?pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=332cb927-0342-42b3-815c-f9124e84021d&pf_rd_r=ZYRJB8E5SN8XG4BRVG76&pf_rd_s=center-1&pf_rd_t=15506&pf_rd_i=tvmeter&ref_=chttvm_tt_77"
## [78] "http://www.imdb.com/title/tt8045468/?pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=332cb927-0342-42b3-815c-f9124e84021d&pf_rd_r=ZYRJB8E5SN8XG4BRVG76&pf_rd_s=center-1&pf_rd_t=15506&pf_rd_i=tvmeter&ref_=chttvm_tt_78"
## [79] "http://www.imdb.com/title/tt0491738/?pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=332cb927-0342-42b3-815c-f9124e84021d&pf_rd_r=ZYRJB8E5SN8XG4BRVG76&pf_rd_s=center-1&pf_rd_t=15506&pf_rd_i=tvmeter&ref_=chttvm_tt_79"
## [80] "http://www.imdb.com/title/tt4524056/?pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=332cb927-0342-42b3-815c-f9124e84021d&pf_rd_r=ZYRJB8E5SN8XG4BRVG76&pf_rd_s=center-1&pf_rd_t=15506&pf_rd_i=tvmeter&ref_=chttvm_tt_80"
## [81] "http://www.imdb.com/title/tt1475582/?pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=332cb927-0342-42b3-815c-f9124e84021d&pf_rd_r=ZYRJB8E5SN8XG4BRVG76&pf_rd_s=center-1&pf_rd_t=15506&pf_rd_i=tvmeter&ref_=chttvm_tt_81"
## [82] "http://www.imdb.com/title/tt0407362/?pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=332cb927-0342-42b3-815c-f9124e84021d&pf_rd_r=ZYRJB8E5SN8XG4BRVG76&pf_rd_s=center-1&pf_rd_t=15506&pf_rd_i=tvmeter&ref_=chttvm_tt_82"
## [83] "http://www.imdb.com/title/tt10361016/?pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=332cb927-0342-42b3-815c-f9124e84021d&pf_rd_r=ZYRJB8E5SN8XG4BRVG76&pf_rd_s=center-1&pf_rd_t=15506&pf_rd_i=tvmeter&ref_=chttvm_tt_83"
## [84] "http://www.imdb.com/title/tt4786824/?pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=332cb927-0342-42b3-815c-f9124e84021d&pf_rd_r=ZYRJB8E5SN8XG4BRVG76&pf_rd_s=center-1&pf_rd_t=15506&pf_rd_i=tvmeter&ref_=chttvm_tt_84"
## [85] "http://www.imdb.com/title/tt1190634/?pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=332cb927-0342-42b3-815c-f9124e84021d&pf_rd_r=ZYRJB8E5SN8XG4BRVG76&pf_rd_s=center-1&pf_rd_t=15506&pf_rd_i=tvmeter&ref_=chttvm_tt_85"
## [86] "http://www.imdb.com/title/tt0460649/?pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=332cb927-0342-42b3-815c-f9124e84021d&pf_rd_r=ZYRJB8E5SN8XG4BRVG76&pf_rd_s=center-1&pf_rd_t=15506&pf_rd_i=tvmeter&ref_=chttvm_tt_86"
## [87] "http://www.imdb.com/title/tt9308346/?pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=332cb927-0342-42b3-815c-f9124e84021d&pf_rd_r=ZYRJB8E5SN8XG4BRVG76&pf_rd_s=center-1&pf_rd_t=15506&pf_rd_i=tvmeter&ref_=chttvm_tt_87"
## [88] "http://www.imdb.com/title/tt3920596/?pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=332cb927-0342-42b3-815c-f9124e84021d&pf_rd_r=ZYRJB8E5SN8XG4BRVG76&pf_rd_s=center-1&pf_rd_t=15506&pf_rd_i=tvmeter&ref_=chttvm_tt_88"
## [89] "http://www.imdb.com/title/tt2805096/?pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=332cb927-0342-42b3-815c-f9124e84021d&pf_rd_r=ZYRJB8E5SN8XG4BRVG76&pf_rd_s=center-1&pf_rd_t=15506&pf_rd_i=tvmeter&ref_=chttvm_tt_89"
## [90] "http://www.imdb.com/title/tt1442437/?pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=332cb927-0342-42b3-815c-f9124e84021d&pf_rd_r=ZYRJB8E5SN8XG4BRVG76&pf_rd_s=center-1&pf_rd_t=15506&pf_rd_i=tvmeter&ref_=chttvm_tt_90"
## [91] "http://www.imdb.com/title/tt10314462/?pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=332cb927-0342-42b3-815c-f9124e84021d&pf_rd_r=ZYRJB8E5SN8XG4BRVG76&pf_rd_s=center-1&pf_rd_t=15506&pf_rd_i=tvmeter&ref_=chttvm_tt_91"
## [92] "http://www.imdb.com/title/tt7456722/?pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=332cb927-0342-42b3-815c-f9124e84021d&pf_rd_r=ZYRJB8E5SN8XG4BRVG76&pf_rd_s=center-1&pf_rd_t=15506&pf_rd_i=tvmeter&ref_=chttvm_tt_92"
## [93] "http://www.imdb.com/title/tt2861424/?pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=332cb927-0342-42b3-815c-f9124e84021d&pf_rd_r=ZYRJB8E5SN8XG4BRVG76&pf_rd_s=center-1&pf_rd_t=15506&pf_rd_i=tvmeter&ref_=chttvm_tt_93"
## [94] "http://www.imdb.com/title/tt2707408/?pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=332cb927-0342-42b3-815c-f9124e84021d&pf_rd_r=ZYRJB8E5SN8XG4BRVG76&pf_rd_s=center-1&pf_rd_t=15506&pf_rd_i=tvmeter&ref_=chttvm_tt_94"
## [95] "http://www.imdb.com/title/tt0412142/?pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=332cb927-0342-42b3-815c-f9124e84021d&pf_rd_r=ZYRJB8E5SN8XG4BRVG76&pf_rd_s=center-1&pf_rd_t=15506&pf_rd_i=tvmeter&ref_=chttvm_tt_95"
## [96] "http://www.imdb.com/title/tt8806524/?pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=332cb927-0342-42b3-815c-f9124e84021d&pf_rd_r=ZYRJB8E5SN8XG4BRVG76&pf_rd_s=center-1&pf_rd_t=15506&pf_rd_i=tvmeter&ref_=chttvm_tt_96"
## [97] "http://www.imdb.com/title/tt7660850/?pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=332cb927-0342-42b3-815c-f9124e84021d&pf_rd_r=ZYRJB8E5SN8XG4BRVG76&pf_rd_s=center-1&pf_rd_t=15506&pf_rd_i=tvmeter&ref_=chttvm_tt_97"
## [98] "http://www.imdb.com/title/tt0096697/?pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=332cb927-0342-42b3-815c-f9124e84021d&pf_rd_r=ZYRJB8E5SN8XG4BRVG76&pf_rd_s=center-1&pf_rd_t=15506&pf_rd_i=tvmeter&ref_=chttvm_tt_98"
## [99] "http://www.imdb.com/title/tt2261391/?pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=332cb927-0342-42b3-815c-f9124e84021d&pf_rd_r=ZYRJB8E5SN8XG4BRVG76&pf_rd_s=center-1&pf_rd_t=15506&pf_rd_i=tvmeter&ref_=chttvm_tt_99"
## [100] "http://www.imdb.com/title/tt4158110/?pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=332cb927-0342-42b3-815c-f9124e84021d&pf_rd_r=ZYRJB8E5SN8XG4BRVG76&pf_rd_s=center-1&pf_rd_t=15506&pf_rd_i=tvmeter&ref_=chttvm_tt_100"
30/45

Automation: Go to each page, scrape show info

  • Programatically direct R to each page on the urls list and run scrape_show_info
scrape_show_info(urls[1])
## # A tibble: 1 x 3
## title runtime genres
## <chr> <chr> <chr>
## 1 Money Heist 1h 10min ""
scrape_show_info(urls[2])
## # A tibble: 1 x 3
## title runtime genres
## <chr> <chr> <chr>
## 1 Ozark 1h Crime, Drama, Thriller
scrape_show_info(urls[3])
## # A tibble: 1 x 3
## title runtime genres
## <chr> <chr> <chr>
## 1 Better Call Saul 46min Crime, Drama
31/45

Go to each page, scrape show info

In other words, we want to map the scrape_show_info function to each element of show_urls:

top_100_shows <- map_df(urls, scrape_show_info)
  • This will hit the urls one after another, and grab the info.
32/45

Passing functions to ... functions?

  • The fact that we can pass a function to another is a big idea, and is one of the things that makes R a functional programming language.
  • It's a bit mind-bending, but it's an idea worth practicing and comfortable with
33/45

aside: lists as an idea: first...vectors

  • c() creates a vector of one type
  • e.g., x <- c(1, 2, 3, "A") contains:
  • [1] "1" "2" "3" "A"
  • class(x) returns:
  • [1] "character"
34/45

aside: lists as an idea: first...vectors

  • You can look up vectors based on position with []
  • x[1] returns the first thing
  • x[2] returns the second thing
  • x[1:2] returns the first through to second thing
35/45

aside: lists as an idea: second...lists

  • list() creates list, which can be any type
y <- list(1,2,3,"x"); y
#> [[1]]
#> [1] 1
#>
#> [[2]]
#> [1] 2
#>
#> [[3]]
#> [1] 3
#>
#> [[4]]
#> [1] "x"
36/45

aside: lists as an idea: second...lists

  • You access positions of a list with [[]]
  • So y[[1]] returns: 1
37/45

aside: a data frame is actually a list!

38/45

calculate the mean for every column:

map(mtcars, mean)
## $mpg
## [1] 20.09062
##
## $cyl
## [1] 6.1875
##
## $disp
## [1] 230.7219
##
## $hp
## [1] 146.6875
##
## $drat
## [1] 3.596563
##
## $wt
## [1] 3.21725
##
## $qsec
## [1] 17.84875
##
## $vs
## [1] 0.4375
##
## $am
## [1] 0.40625
##
## $gear
## [1] 3.6875
##
## $carb
## [1] 2.8125
39/45

calculate the mean for every column:

map_dbl(mtcars, mean)
## mpg cyl disp hp drat wt qsec
## 20.090625 6.187500 230.721875 146.687500 3.596563 3.217250 17.848750
## vs am gear carb
## 0.437500 0.406250 3.687500 2.812500
40/45

Range for every column: writing a function

my_range <- function(x){
max(x) - min(x)
}
map_dbl(mtcars, my_range)
## mpg cyl disp hp drat wt qsec vs am
## 23.500 4.000 400.900 283.000 2.170 3.911 8.400 1.000 1.000
## gear carb
## 2.000 7.000
41/45

Range for every column: writing a function in map

map_dbl(mtcars, .f = function(x) max(x) - min(x))
## mpg cyl disp hp drat wt qsec vs am
## 23.500 4.000 400.900 283.000 2.170 3.911 8.400 1.000 1.000
## gear carb
## 2.000 7.000
42/45

Range for every column: writing a function in map

map_dbl(mtcars, .f = ~(max(.) - min(.)))
## mpg cyl disp hp drat wt qsec vs am
## 23.500 4.000 400.900 283.000 2.170 3.911 8.400 1.000 1.000
## gear carb
## 2.000 7.000
43/45

Your Turn: rstudio.cloud

Take the lab quiz!

44/45

Resources

45/45


ETC1010: Introduction to Data Analysis

Week 6, part B


Functions

Lecturer: Nicholas Tierney

Department of Econometrics and Business Statistics

ETC1010.Clayton-x@monash.edu

April 2020


1/45
Paused

Help

Keyboard shortcuts

, , Pg Up, k Go to previous slide
, , Pg Dn, Space, j Go to next slide
Home Go to first slide
End Go to last slide
Number + Return Go to specific slide
b / m / f Toggle blackout / mirrored / fullscreen mode
c Clone slideshow
p Toggle presenter mode
t Restart the presentation timer
?, h Toggle this help
Esc Back to slideshow