Packages

library(tidyverse)
library(repurrrsive)
library(rjson)
library(janitor)

Exercise 1

Problem

Consider a tibble of data filtered from world_bank_pop. This dataset is included in package tidyr.

usa_pop <- world_bank_pop %>% 
  filter(country == "USA")

Tidy usa_pop so it looks like the tibble below. See ?world_bank_pop for a description of the variables and their values.

Solution

usa_pop %>% 
  pivot_longer(cols = `2000`:`2017`, names_to = "year", values_to = "value") %>% 
  pivot_wider(names_from = indicator, values_from = value) %>% 
  clean_names()

Exercise 2

Problem

Using sw_people in package repurrrsive, extract the name of all characters using:

  • a for loop,

  • an apply function.

str(sw_people[[1]])
#> List of 16
#>  $ name      : chr "Luke Skywalker"
#>  $ height    : chr "172"
#>  $ mass      : chr "77"
#>  $ hair_color: chr "blond"
#>  $ skin_color: chr "fair"
#>  $ eye_color : chr "blue"
#>  $ birth_year: chr "19BBY"
#>  $ gender    : chr "male"
#>  $ homeworld : chr "http://swapi.co/api/planets/1/"
#>  $ films     : chr [1:5] "http://swapi.co/api/films/6/" "http://swapi.co/api/films/3/" "http://swapi.co/api/films/2/" "http://swapi.co/api/films/1/" ...
#>  $ species   : chr "http://swapi.co/api/species/1/"
#>  $ vehicles  : chr [1:2] "http://swapi.co/api/vehicles/14/" "http://swapi.co/api/vehicles/30/"
#>  $ starships : chr [1:2] "http://swapi.co/api/starships/12/" "http://swapi.co/api/starships/22/"
#>  $ created   : chr "2014-12-09T13:50:51.644000Z"
#>  $ edited    : chr "2014-12-20T21:17:56.891000Z"
#>  $ url       : chr "http://swapi.co/api/people/1/"

Hint: The [ and [[ are functions.

Solution

Using a for loop:

out <- character(length = length(sw_people))

for (i in seq_along(sw_people)) {
  out[i] <- sw_people[[i]]$name
}

Using sapply():

s_out <- sapply(sw_people, `[[`, "name")

Exercise 3

Problem

Use mtcars and a single map or map variant to

  1. get the type of each variable,

  2. get the fourth row such that result is a character vector,

  3. compute the mean of each variable, and

  4. compute the mean and median for each variable such that the result is a data frame with the mean values in row 1 and the median values in row 2.

Solution

The solutions to 1, 2, 3, 4 are given below by each line of code, respectively.

map_chr(mtcars, typeof)
#>      mpg      cyl     disp       hp     drat       wt     qsec       vs 
#> "double" "double" "double" "double" "double" "double" "double" "double" 
#>       am     gear     carb 
#> "double" "double" "double"
map_chr(mtcars, 4)
#>          mpg          cyl         disp           hp         drat 
#>  "21.400000"   "6.000000" "258.000000" "110.000000"   "3.080000" 
#>           wt         qsec           vs           am         gear 
#>   "3.215000"  "19.440000"   "1.000000"   "0.000000"   "3.000000" 
#>         carb 
#>   "1.000000"
map_dbl(mtcars, mean)
#>        mpg        cyl       disp         hp       drat         wt 
#>  20.090625   6.187500 230.721875 146.687500   3.596563   3.217250 
#>       qsec         vs         am       gear       carb 
#>  17.848750   0.437500   0.406250   3.687500   2.812500
map_df(mtcars, ~ c(mean(.), median(.)))

Exercise 4

Consider the object senators below.

json_file <- "https://www.govtrack.us/api/v2/role?current=true&role_type=senator"
senators <- fromJSON(paste(readLines(json_file), collapse = ""))

Problem

Use one of the map_*() variants to get

  1. the name of each senator as a character vector (preview given below),
#> [1] "Sen. Lamar Alexander [R-TN]" "Sen. Susan Collins [R-ME]"  
#> [3] "Sen. John Cornyn [R-TX]"     "Sen. Richard Durbin [D-IL]" 
#> [5] "Sen. Michael Enzi [R-WY]"
  1. the name of each senator as a list (preview given below),
#> [[1]]
#> [1] "Sen. Lamar Alexander [R-TN]"
#> 
#> [[2]]
#> [1] "Sen. Susan Collins [R-ME]"
#> 
#> [[3]]
#> [1] "Sen. John Cornyn [R-TX]"
  1. the description and party of each senator as a data frame (preview given below)

Solution

Part 1

map_chr(senators$objects, list("person", "name"))

Part 2

map(senators$objects, list("person", "name"))[1:3]

Part 3

map_df(senators$objects, `[`, c("description", "party")) %>% 
  slice(1:4)