Data Manipulation in the tidyverse¶
Load library¶
[1]:
library(tidyverse)
Registered S3 methods overwritten by 'ggplot2':
method from
[.quosures rlang
c.quosures rlang
print.quosures rlang
── Attaching packages ─────────────────────────────────────── tidyverse 1.2.1 ──
✔ ggplot2 3.1.1 ✔ purrr 0.3.2
✔ tibble 2.1.2 ✔ dplyr 0.8.1
✔ tidyr 0.8.3 ✔ stringr 1.4.0
✔ readr 1.3.1 ✔ forcats 0.4.0
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag() masks stats::lag()
Bssic information¶
[4]:
class(starwars)
- 'tbl_df'
- 'tbl'
- 'data.frame'
[5]:
dim(starwars)
- 87
- 13
[6]:
nrow(starwars)
87
[7]:
ncol(starwars)
13
[8]:
head(starwars, 3)
| name | height | mass | hair_color | skin_color | eye_color | birth_year | gender | homeworld | species | films | vehicles | starships |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| <chr> | <int> | <dbl> | <chr> | <chr> | <chr> | <dbl> | <chr> | <chr> | <chr> | <list> | <list> | <list> |
| Luke Skywalker | 172 | 77 | blond | fair | blue | 19 | male | Tatooine | Human | Revenge of the Sith , Return of the Jedi , The Empire Strikes Back, A New Hope , The Force Awakens | Snowspeeder , Imperial Speeder Bike | X-wing , Imperial shuttle |
| C-3PO | 167 | 75 | NA | gold | yellow | 112 | NA | Tatooine | Droid | Attack of the Clones , The Phantom Menace , Revenge of the Sith , Return of the Jedi , The Empire Strikes Back, A New Hope | ||
| R2-D2 | 96 | 32 | NA | white, blue | red | 33 | NA | Naboo | Droid | Attack of the Clones , The Phantom Menace , Revenge of the Sith , Return of the Jedi , The Empire Strikes Back, A New Hope , The Force Awakens |
[9]:
tail(starwars, 3)
| name | height | mass | hair_color | skin_color | eye_color | birth_year | gender | homeworld | species | films | vehicles | starships |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| <chr> | <int> | <dbl> | <chr> | <chr> | <chr> | <dbl> | <chr> | <chr> | <chr> | <list> | <list> | <list> |
| BB8 | NA | NA | none | none | black | NA | none | NA | Droid | The Force Awakens | ||
| Captain Phasma | NA | NA | unknown | unknown | unknown | NA | female | NA | NA | The Force Awakens | ||
| Padmé Amidala | 165 | 45 | brown | light | brown | 46 | female | Naboo | Human | Attack of the Clones, The Phantom Menace , Revenge of the Sith | H-type Nubian yacht, Naboo star skiff , Naboo fighter |
[10]:
sample_n(starwars, 3)
| name | height | mass | hair_color | skin_color | eye_color | birth_year | gender | homeworld | species | films | vehicles | starships |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| <chr> | <int> | <dbl> | <chr> | <chr> | <chr> | <dbl> | <chr> | <chr> | <chr> | <list> | <list> | <list> |
| Sly Moore | 178 | 48 | none | pale | white | NA | female | Umbara | NA | Attack of the Clones, Revenge of the Sith | ||
| Bail Prestor Organa | 191 | NA | black | tan | brown | 67 | male | Alderaan | Human | Attack of the Clones, Revenge of the Sith | ||
| Ben Quadinaros | 163 | 65 | none | grey, green, yellow | orange | NA | male | Tund | Toong | The Phantom Menace |
Piping¶
[11]:
starwars %>% head(5) %>% tail(3)
| name | height | mass | hair_color | skin_color | eye_color | birth_year | gender | homeworld | species | films | vehicles | starships |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| <chr> | <int> | <dbl> | <chr> | <chr> | <chr> | <dbl> | <chr> | <chr> | <chr> | <list> | <list> | <list> |
| R2-D2 | 96 | 32 | NA | white, blue | red | 33.0 | NA | Naboo | Droid | Attack of the Clones , The Phantom Menace , Revenge of the Sith , Return of the Jedi , The Empire Strikes Back, A New Hope , The Force Awakens | ||
| Darth Vader | 202 | 136 | none | white | yellow | 41.9 | male | Tatooine | Human | Revenge of the Sith , Return of the Jedi , The Empire Strikes Back, A New Hope | TIE Advanced x1 | |
| Leia Organa | 150 | 49 | brown | light | brown | 19.0 | female | Alderaan | Human | Revenge of the Sith , Return of the Jedi , The Empire Strikes Back, A New Hope , The Force Awakens | Imperial Speeder Bike |
[12]:
starwars %>%
head(5) %>%
tail(3)
| name | height | mass | hair_color | skin_color | eye_color | birth_year | gender | homeworld | species | films | vehicles | starships |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| <chr> | <int> | <dbl> | <chr> | <chr> | <chr> | <dbl> | <chr> | <chr> | <chr> | <list> | <list> | <list> |
| R2-D2 | 96 | 32 | NA | white, blue | red | 33.0 | NA | Naboo | Droid | Attack of the Clones , The Phantom Menace , Revenge of the Sith , Return of the Jedi , The Empire Strikes Back, A New Hope , The Force Awakens | ||
| Darth Vader | 202 | 136 | none | white | yellow | 41.9 | male | Tatooine | Human | Revenge of the Sith , Return of the Jedi , The Empire Strikes Back, A New Hope | TIE Advanced x1 | |
| Leia Organa | 150 | 49 | brown | light | brown | 19.0 | female | Alderaan | Human | Revenge of the Sith , Return of the Jedi , The Empire Strikes Back, A New Hope , The Force Awakens | Imperial Speeder Bike |
Select¶
[13]:
starwars %>%
select(name, birth_year, species) %>%
head(3)
| name | birth_year | species |
|---|---|---|
| <chr> | <dbl> | <chr> |
| Luke Skywalker | 19 | Human |
| C-3PO | 112 | Droid |
| R2-D2 | 33 | Droid |
[14]:
starwars %>%
select(name, age=birth_year, species) %>%
head(3)
| name | age | species |
|---|---|---|
| <chr> | <dbl> | <chr> |
| Luke Skywalker | 19 | Human |
| C-3PO | 112 | Droid |
| R2-D2 | 33 | Droid |
[15]:
starwars %>%
select(-name, -birth_year, -species) %>%
head(3)
| height | mass | hair_color | skin_color | eye_color | gender | homeworld | films | vehicles | starships |
|---|---|---|---|---|---|---|---|---|---|
| <int> | <dbl> | <chr> | <chr> | <chr> | <chr> | <chr> | <list> | <list> | <list> |
| 172 | 77 | blond | fair | blue | male | Tatooine | Revenge of the Sith , Return of the Jedi , The Empire Strikes Back, A New Hope , The Force Awakens | Snowspeeder , Imperial Speeder Bike | X-wing , Imperial shuttle |
| 167 | 75 | NA | gold | yellow | NA | Tatooine | Attack of the Clones , The Phantom Menace , Revenge of the Sith , Return of the Jedi , The Empire Strikes Back, A New Hope | ||
| 96 | 32 | NA | white, blue | red | NA | Naboo | Attack of the Clones , The Phantom Menace , Revenge of the Sith , Return of the Jedi , The Empire Strikes Back, A New Hope , The Force Awakens |
[16]:
starwars%>%
select(starts_with('s')) %>%
head(3)
| skin_color | species | starships |
|---|---|---|
| <chr> | <chr> | <list> |
| fair | Human | X-wing , Imperial shuttle |
| gold | Droid | |
| white, blue | Droid |
[17]:
starwars%>%
select(matches("[aeiou]{2,}")) %>%
head(3)
| height | hair_color | birth_year | species |
|---|---|---|---|
| <int> | <chr> | <dbl> | <chr> |
| 172 | blond | 19 | Human |
| 167 | NA | 112 | Droid |
| 96 | NA | 33 | Droid |
Arrange¶
[18]:
starwars%>%
select(name, age=birth_year, mass) %>%
arrange(age) %>%
head(3)
| name | age | mass |
|---|---|---|
| <chr> | <dbl> | <dbl> |
| Wicket Systri Warrick | 8 | 20 |
| IG-88 | 15 | 140 |
| Luke Skywalker | 19 | 77 |
[19]:
starwars%>%
select(name, age=birth_year, mass) %>%
arrange(desc(age)) %>%
head(3)
| name | age | mass |
|---|---|---|
| <chr> | <dbl> | <dbl> |
| Yoda | 896 | 17 |
| Jabba Desilijic Tiure | 600 | 1358 |
| Chewbacca | 200 | 112 |
[20]:
starwars%>%
select(name, age=birth_year, mass) %>%
arrange(age) %>%
head(5)
| name | age | mass |
|---|---|---|
| <chr> | <dbl> | <dbl> |
| Wicket Systri Warrick | 8 | 20 |
| IG-88 | 15 | 140 |
| Luke Skywalker | 19 | 77 |
| Leia Organa | 19 | 49 |
| Wedge Antilles | 21 | 77 |
[21]:
starwars%>%
select(name, age=birth_year, mass) %>%
arrange(age, mass) %>%
head(5)
| name | age | mass |
|---|---|---|
| <chr> | <dbl> | <dbl> |
| Wicket Systri Warrick | 8 | 20 |
| IG-88 | 15 | 140 |
| Leia Organa | 19 | 49 |
| Luke Skywalker | 19 | 77 |
| Wedge Antilles | 21 | 77 |
[22]:
starwars%>%
select(name, age=birth_year, mass) %>%
top_n(age, n=3)
| name | age | mass |
|---|---|---|
| <chr> | <dbl> | <dbl> |
| Chewbacca | 200 | 112 |
| Jabba Desilijic Tiure | 600 | 1358 |
| Yoda | 896 | 17 |
Filter¶
[23]:
starwars %>%
filter(birth_year > 100)
| name | height | mass | hair_color | skin_color | eye_color | birth_year | gender | homeworld | species | films | vehicles | starships |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| <chr> | <int> | <dbl> | <chr> | <chr> | <chr> | <dbl> | <chr> | <chr> | <chr> | <list> | <list> | <list> |
| C-3PO | 167 | 75 | NA | gold | yellow | 112 | NA | Tatooine | Droid | Attack of the Clones , The Phantom Menace , Revenge of the Sith , Return of the Jedi , The Empire Strikes Back, A New Hope | ||
| Chewbacca | 228 | 112 | brown | unknown | blue | 200 | male | Kashyyyk | Wookiee | Revenge of the Sith , Return of the Jedi , The Empire Strikes Back, A New Hope , The Force Awakens | AT-ST | Millennium Falcon, Imperial shuttle |
| Jabba Desilijic Tiure | 175 | 1358 | NA | green-tan, brown | orange | 600 | hermaphrodite | Nal Hutta | Hutt | The Phantom Menace, Return of the Jedi, A New Hope | ||
| Yoda | 66 | 17 | white | green | brown | 896 | male | NA | Yoda's species | Attack of the Clones , The Phantom Menace , Revenge of the Sith , Return of the Jedi , The Empire Strikes Back | ||
| Dooku | 193 | 80 | white | fair | brown | 102 | male | Serenno | Human | Attack of the Clones, Revenge of the Sith | Flitknot speeder |
[24]:
starwars %>%
filter((birth_year >= 600) | (name == 'Dooku'))
| name | height | mass | hair_color | skin_color | eye_color | birth_year | gender | homeworld | species | films | vehicles | starships |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| <chr> | <int> | <dbl> | <chr> | <chr> | <chr> | <dbl> | <chr> | <chr> | <chr> | <list> | <list> | <list> |
| Jabba Desilijic Tiure | 175 | 1358 | NA | green-tan, brown | orange | 600 | hermaphrodite | Nal Hutta | Hutt | The Phantom Menace, Return of the Jedi, A New Hope | ||
| Yoda | 66 | 17 | white | green | brown | 896 | male | NA | Yoda's species | Attack of the Clones , The Phantom Menace , Revenge of the Sith , Return of the Jedi , The Empire Strikes Back | ||
| Dooku | 193 | 80 | white | fair | brown | 102 | male | Serenno | Human | Attack of the Clones, Revenge of the Sith | Flitknot speeder |
[25]:
starwars %>%
filter((birth_year >= 600) | (name == 'Dooku')) %>%
select(name, age=birth_year, homeworld)
| name | age | homeworld |
|---|---|---|
| <chr> | <dbl> | <chr> |
| Jabba Desilijic Tiure | 600 | Nal Hutta |
| Yoda | 896 | NA |
| Dooku | 102 | Serenno |
[26]:
starwars %>%
filter(birth_year > 100, gender=='male')
| name | height | mass | hair_color | skin_color | eye_color | birth_year | gender | homeworld | species | films | vehicles | starships |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| <chr> | <int> | <dbl> | <chr> | <chr> | <chr> | <dbl> | <chr> | <chr> | <chr> | <list> | <list> | <list> |
| Chewbacca | 228 | 112 | brown | unknown | blue | 200 | male | Kashyyyk | Wookiee | Revenge of the Sith , Return of the Jedi , The Empire Strikes Back, A New Hope , The Force Awakens | AT-ST | Millennium Falcon, Imperial shuttle |
| Yoda | 66 | 17 | white | green | brown | 896 | male | NA | Yoda's species | Attack of the Clones , The Phantom Menace , Revenge of the Sith , Return of the Jedi , The Empire Strikes Back | ||
| Dooku | 193 | 80 | white | fair | brown | 102 | male | Serenno | Human | Attack of the Clones, Revenge of the Sith | Flitknot speeder |
[27]:
starwars %>%
filter(str_detect(homeworld, "in")) %>%
head(3)
| name | height | mass | hair_color | skin_color | eye_color | birth_year | gender | homeworld | species | films | vehicles | starships |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| <chr> | <int> | <dbl> | <chr> | <chr> | <chr> | <dbl> | <chr> | <chr> | <chr> | <list> | <list> | <list> |
| Luke Skywalker | 172 | 77 | blond | fair | blue | 19.0 | male | Tatooine | Human | Revenge of the Sith , Return of the Jedi , The Empire Strikes Back, A New Hope , The Force Awakens | Snowspeeder , Imperial Speeder Bike | X-wing , Imperial shuttle |
| C-3PO | 167 | 75 | NA | gold | yellow | 112.0 | NA | Tatooine | Droid | Attack of the Clones , The Phantom Menace , Revenge of the Sith , Return of the Jedi , The Empire Strikes Back, A New Hope | ||
| Darth Vader | 202 | 136 | none | white | yellow | 41.9 | male | Tatooine | Human | Revenge of the Sith , Return of the Jedi , The Empire Strikes Back, A New Hope | TIE Advanced x1 |
Mutate¶
[28]:
starwars%>%
select(name, age=birth_year, height, mass) %>%
head(3)
| name | age | height | mass |
|---|---|---|---|
| <chr> | <dbl> | <int> | <dbl> |
| Luke Skywalker | 19 | 172 | 77 |
| C-3PO | 112 | 167 | 75 |
| R2-D2 | 33 | 96 | 32 |
[29]:
starwars%>%
select(name, age=birth_year, height, mass) %>%
mutate(bmi=mass/(height/100)^2, obese=bmi>30) %>%
head(3)
| name | age | height | mass | bmi | obese |
|---|---|---|---|---|---|
| <chr> | <dbl> | <int> | <dbl> | <dbl> | <lgl> |
| Luke Skywalker | 19 | 172 | 77 | 26.02758 | FALSE |
| C-3PO | 112 | 167 | 75 | 26.89232 | FALSE |
| R2-D2 | 33 | 96 | 32 | 34.72222 | TRUE |
[30]:
starwars%>%
select(name, age=birth_year, height, mass) %>%
transmute(bmi=mass/(height/100)^2, obese=bmi>30) %>%
head(3)
| bmi | obese |
|---|---|
| <dbl> | <lgl> |
| 26.02758 | FALSE |
| 26.89232 | FALSE |
| 34.72222 | TRUE |
[31]:
starwars %>%
transmute_if(is_character, str_to_upper) %>%
head(3)
| name | hair_color | skin_color | eye_color | gender | homeworld | species |
|---|---|---|---|---|---|---|
| <chr> | <chr> | <chr> | <chr> | <chr> | <chr> | <chr> |
| LUKE SKYWALKER | BLOND | FAIR | BLUE | MALE | TATOOINE | HUMAN |
| C-3PO | NA | GOLD | YELLOW | NA | TATOOINE | DROID |
| R2-D2 | NA | WHITE, BLUE | RED | NA | NABOO | DROID |
[32]:
starwars %>%
select_at(c("birth_year", "height", "mass")) %>%
head(3)
| birth_year | height | mass |
|---|---|---|
| <dbl> | <int> | <dbl> |
| 19 | 172 | 77 |
| 112 | 167 | 75 |
| 33 | 96 | 32 |
[33]:
starwars %>%
transmute_at(c("birth_year", "height", "mass"), ~ . + 1) %>%
head(3)
| birth_year | height | mass |
|---|---|---|
| <dbl> | <dbl> | <dbl> |
| 20 | 173 | 78 |
| 113 | 168 | 76 |
| 34 | 97 | 33 |
Summarize¶
[34]:
starwars %>%
summarize(avg_mass = mean(mass, na.rm=TRUE),
median_age=median(birth_year, na.rm=TRUE))
| avg_mass | median_age |
|---|---|
| <dbl> | <dbl> |
| 97.31186 | 52 |
[35]:
starwars %>%
summarize_if(is.numeric, mean, na.rm=TRUE)
| height | mass | birth_year |
|---|---|---|
| <dbl> | <dbl> | <dbl> |
| 174.358 | 97.31186 | 87.56512 |
[36]:
starwars %>%
summarize_if(is.numeric, list(mean, median), na.rm=TRUE)
| height_fn1 | mass_fn1 | birth_year_fn1 | height_fn2 | mass_fn2 | birth_year_fn2 |
|---|---|---|---|---|---|
| <dbl> | <dbl> | <dbl> | <int> | <dbl> | <dbl> |
| 174.358 | 97.31186 | 87.56512 | 180 | 79 | 52 |
Group by¶
[37]:
starwars %>%
group_by(homeworld) %>%
summarize(avg_mass=mean(mass, na.rm=T)) %>%
head(3)
| homeworld | avg_mass |
|---|---|
| <chr> | <dbl> |
| NA | 82 |
| Alderaan | 64 |
| Aleen Minor | 15 |
[38]:
starwars %>%
group_by(homeworld) %>%
summarize(avg_mass=mean(mass, na.rm=T)) %>%
filter(!is.na(homeworld)) %>%
head(3)
| homeworld | avg_mass |
|---|---|
| <chr> | <dbl> |
| Alderaan | 64 |
| Aleen Minor | 15 |
| Bespin | 79 |
[39]:
starwars %>%
group_by(homeworld, species, gender) %>%
summarize(avg_mass=mean(mass, na.rm=T), n=n()) %>%
filter(!is.na(homeworld)) %>%
head(5)
| homeworld | species | gender | avg_mass | n |
|---|---|---|---|---|
| <chr> | <chr> | <chr> | <dbl> | <int> |
| Alderaan | Human | female | 49 | 1 |
| Alderaan | Human | male | 79 | 2 |
| Aleen Minor | Aleena | male | 15 | 1 |
| Bespin | Human | male | 79 | 1 |
| Bestine IV | Human | male | 110 | 1 |
Exercises¶
1. Show the name, height and mass of all Droids sorted by height from tallest to shortest.
[40]:
starwars %>%
filter(species == 'Droid') %>%
arrange(desc(height)) %>%
select(name, height, mass)
| name | height | mass |
|---|---|---|
| <chr> | <int> | <dbl> |
| IG-88 | 200 | 140 |
| C-3PO | 167 | 75 |
| R5-D4 | 97 | 32 |
| R2-D2 | 96 | 32 |
| BB8 | NA | NA |
2. List the name and hoemworld of all non-humans who appeared in The Empire Strikes Back.
[41]:
starwars %>%
filter(str_detect(films, 'The Empire Strikes Back'), species!='Human') %>%
select(name, homeworld)
Warning message in stri_detect_regex(string, pattern, negate = negate, opts_regex = opts(pattern)):
“argument is not an atomic vector; coercing”
| name | homeworld |
|---|---|
| <chr> | <chr> |
| C-3PO | Tatooine |
| R2-D2 | Naboo |
| Chewbacca | Kashyyyk |
| Yoda | NA |
| IG-88 | NA |
| Bossk | Trandosha |
3. Which homeworld has the most humans?
[42]:
starwars %>%
filter(species == "Human") %>%
group_by(homeworld) %>%
summarize(n=n()) %>%
top_n(1)
Selecting by n
| homeworld | n |
|---|---|
| <chr> | <int> |
| Tatooine | 8 |
4. What species has the highest average BMI? Exclude Species with only 1 member, and ignore NAs in the calculation of the average.
[43]:
starwars %>%
mutate(bmi = mass/(height/100)^2) %>%
group_by(species) %>%
summarize(avg_bmi=mean(bmi, na.rm=T), n=n()) %>%
filter(n>1) %>%
top_n(avg_bmi, n=1)
| species | avg_bmi | n |
|---|---|---|
| <chr> | <dbl> | <int> |
| Droid | 32.65613 | 5 |
[ ]: