# 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)) {

map(senators$objects, list("person", "name"))[1:3] #### Part 3 map_df(senators$objects, [, c("description", "party")) %>%
slice(1:4)