Some of the code from Chapter 4, Section 1.

In this chapter dplyr is introduced. We will be using dplyr all year.

The main idea of data wrangling with dplyr are the 5 verbs.

select() # take a subset of columns

filter() # take a subset of rows

mutate() # add or modify existing columns

arrange() # sort the rows

summarize() # aggregate the data across rows

The dplyr package is part of the tidyverse. We will install and load the tidyverse.

library(mdsr)
library(tidyverse)

Star Wars dataset

data("starwars")
glimpse(starwars)
Observations: 87
Variables: 13
$ name       <chr> "Luke Skywalker", "C-3PO", "R2-D2", "Darth Vader", "Leia ...
$ height     <int> 172, 167, 96, 202, 150, 178, 165, 97, 183, 182, 188, 180,...
$ mass       <dbl> 77.0, 75.0, 32.0, 136.0, 49.0, 120.0, 75.0, 32.0, 84.0, 7...
$ hair_color <chr> "blond", NA, NA, "none", "brown", "brown, grey", "brown",...
$ skin_color <chr> "fair", "gold", "white, blue", "white", "light", "light",...
$ eye_color  <chr> "blue", "yellow", "red", "yellow", "brown", "blue", "blue...
$ birth_year <dbl> 19.0, 112.0, 33.0, 41.9, 19.0, 52.0, 47.0, NA, 24.0, 57.0...
$ gender     <chr> "male", NA, NA, "male", "female", "male", "female", NA, "...
$ homeworld  <chr> "Tatooine", "Tatooine", "Naboo", "Tatooine", "Alderaan", ...
$ species    <chr> "Human", "Droid", "Droid", "Human", "Human", "Human", "Hu...
$ films      <list> [<"Revenge of the Sith", "Return of the Jedi", "The Empi...
$ vehicles   <list> [<"Snowspeeder", "Imperial Speeder Bike">, <>, <>, <>, "...
$ starships  <list> [<"X-wing", "Imperial shuttle">, <>, <>, "TIE Advanced x...

select()

starwars %>% select(name, species)

filter()

starwars %>% 
  filter(species == "Droid")

select()

starwars %>% 
  select(name, ends_with("color"))

mutate()

starwars %>% 
  mutate(name, bmi = mass / ((height / 100)  ^ 2)) %>%
  select(name:mass, bmi)

arrange()

starwars %>% 
  arrange(desc(mass))

summarize()

starwars %>%
  group_by(species) %>%
  summarise(
    n = n(),
    mass = mean(mass, na.rm = TRUE)
  ) %>%
  filter(n > 1)

Questions

Develop the R code to answer the following questions.

  1. How many films are in the dataset?
  2. Are there more Droids or humans in the Star Wars movies?
  3. Which of the Star Wars movies was Luke Skywalker in?
  4. Pose a question and answer it by wrangling the starwars dataset.

Presidential examples

Try out the code in Chapter 4 Section 1 using the presidential data set.

presidential

Star Wars API and R package

More Star Wars stuff you might find interesting.

  • Check out the Star Wars website.
  • Check out the Star Wars API sawpi.
  • And check out the R package rwars.

rwars package

This is a package that connects to the sawpi to pull data from the API.

If the package does not install from CRAN you can isntall it from github.

library(devtools)
install_github("ironholds/rwars")
library(rwars)
planet_schema <- get_planet_schema()
names(planet_schema)
[1] "required"    "description" "title"       "$schema"     "properties"  "type"       

rwars package

Get an individual starship - an X-wing.

Hopefully it won’t time out and will actually bring the data back.

x_wing <- get_starship(12)
x_wing
$name
[1] "X-wing"

$model
[1] "T-65 X-wing"

$manufacturer
[1] "Incom Corporation"

$cost_in_credits
[1] "149999"

$length
[1] "12.5"

$max_atmosphering_speed
[1] "1050"

$crew
[1] "1"

$passengers
[1] "0"

$cargo_capacity
[1] "110"

$consumables
[1] "1 week"

$hyperdrive_rating
[1] "1.0"

$MGLT
[1] "100"

$starship_class
[1] "Starfighter"

$pilots
$pilots[[1]]
[1] "https://swapi.co/api/people/1/"

$pilots[[2]]
[1] "https://swapi.co/api/people/9/"

$pilots[[3]]
[1] "https://swapi.co/api/people/18/"

$pilots[[4]]
[1] "https://swapi.co/api/people/19/"


$films
$films[[1]]
[1] "https://swapi.co/api/films/2/"

$films[[2]]
[1] "https://swapi.co/api/films/3/"

$films[[3]]
[1] "https://swapi.co/api/films/1/"


$created
[1] "2014-12-12T11:19:05.340000Z"

$edited
[1] "2014-12-22T17:35:44.491233Z"

$url
[1] "https://swapi.co/api/starships/12/"
LS0tCnRpdGxlOiAiRGF0YSBXcmFuZ2xpbmcgUiIKYXV0aG9yOiAiUHJvZi4gRXJpYyBBLiBTdWVzcyIKb3V0cHV0OgogIHdvcmRfZG9jdW1lbnQ6IGRlZmF1bHQK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