library (dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
head(starwars)
## # A tibble: 6 × 14
## name height mass hair_color skin_color eye_color birth_year sex gender
## <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> <chr>
## 1 Luke Sky… 172 77 blond fair blue 19 male mascu…
## 2 C-3PO 167 75 <NA> gold yellow 112 none mascu…
## 3 R2-D2 96 32 <NA> white, bl… red 33 none mascu…
## 4 Darth Va… 202 136 none white yellow 41.9 male mascu…
## 5 Leia Org… 150 49 brown light brown 19 fema… femin…
## 6 Owen Lars 178 120 brown, gr… light blue 52 male mascu…
## # ℹ 5 more variables: homeworld <chr>, species <chr>, films <list>,
## # vehicles <list>, starships <list>
glimpse (starwars)
## Rows: 87
## Columns: 14
## $ name <chr> "Luke Skywalker", "C-3PO", "R2-D2", "Darth Vader", "Leia Or…
## $ height <int> 172, 167, 96, 202, 150, 178, 165, 97, 183, 182, 188, 180, 2…
## $ mass <dbl> 77.0, 75.0, 32.0, 136.0, 49.0, 120.0, 75.0, 32.0, 84.0, 77.…
## $ hair_color <chr> "blond", NA, NA, "none", "brown", "brown, grey", "brown", N…
## $ 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, …
## $ sex <chr> "male", "none", "none", "male", "female", "male", "female",…
## $ gender <chr> "masculine", "masculine", "masculine", "masculine", "femini…
## $ homeworld <chr> "Tatooine", "Tatooine", "Naboo", "Tatooine", "Alderaan", "T…
## $ species <chr> "Human", "Droid", "Droid", "Human", "Human", "Human", "Huma…
## $ films <list> <"A New Hope", "The Empire Strikes Back", "Return of the J…
## $ vehicles <list> <"Snowspeeder", "Imperial Speeder Bike">, <>, <>, <>, "Imp…
## $ starships <list> <"X-wing", "Imperial shuttle">, <>, <>, "TIE Advanced x1",…
starwars_selected <- starwars %>%
select(name,height,mass,sex,species)
starwars_selected
## # A tibble: 87 × 5
## name height mass sex species
## <chr> <int> <dbl> <chr> <chr>
## 1 Luke Skywalker 172 77 male Human
## 2 C-3PO 167 75 none Droid
## 3 R2-D2 96 32 none Droid
## 4 Darth Vader 202 136 male Human
## 5 Leia Organa 150 49 female Human
## 6 Owen Lars 178 120 male Human
## 7 Beru Whitesun Lars 165 75 female Human
## 8 R5-D4 97 32 none Droid
## 9 Biggs Darklighter 183 84 male Human
## 10 Obi-Wan Kenobi 182 77 male Human
## # ℹ 77 more rows
starwars_filtered <- starwars_selected %>%
filter(height > 150, mass > 50)
starwars_filtered
## # A tibble: 46 × 5
## name height mass sex species
## <chr> <int> <dbl> <chr> <chr>
## 1 Luke Skywalker 172 77 male Human
## 2 C-3PO 167 75 none Droid
## 3 Darth Vader 202 136 male Human
## 4 Owen Lars 178 120 male Human
## 5 Beru Whitesun Lars 165 75 female Human
## 6 Biggs Darklighter 183 84 male Human
## 7 Obi-Wan Kenobi 182 77 male Human
## 8 Anakin Skywalker 188 84 male Human
## 9 Chewbacca 228 112 male Wookiee
## 10 Han Solo 180 80 male Human
## # ℹ 36 more rows
starwars_summary <- starwars_selected %>%
summarize (
avg_height = mean(height, na.rm = TRUE),
avg_mass = mean(mass, na.rm = TRUE))
starwars_summary
## # A tibble: 1 × 2
## avg_height avg_mass
## <dbl> <dbl>
## 1 175. 97.3
starwars_arranged_desc <- starwars_selected %>%
arrange(desc(height))
starwars_arranged_desc
## # A tibble: 87 × 5
## name height mass sex species
## <chr> <int> <dbl> <chr> <chr>
## 1 Yarael Poof 264 NA male Quermian
## 2 Tarfful 234 136 male Wookiee
## 3 Lama Su 229 88 male Kaminoan
## 4 Chewbacca 228 112 male Wookiee
## 5 Roos Tarpals 224 82 male Gungan
## 6 Grievous 216 159 male Kaleesh
## 7 Taun We 213 NA female Kaminoan
## 8 Rugor Nass 206 NA male Gungan
## 9 Tion Medon 206 80 male Pau'an
## 10 Darth Vader 202 136 male Human
## # ℹ 77 more rows
starwars_bmi <- starwars_selected %>%
mutate(
height_m = height / 100,
bmi = mass / (height_m^2))
starwars_bmi
## # A tibble: 87 × 7
## name height mass sex species height_m bmi
## <chr> <int> <dbl> <chr> <chr> <dbl> <dbl>
## 1 Luke Skywalker 172 77 male Human 1.72 26.0
## 2 C-3PO 167 75 none Droid 1.67 26.9
## 3 R2-D2 96 32 none Droid 0.96 34.7
## 4 Darth Vader 202 136 male Human 2.02 33.3
## 5 Leia Organa 150 49 female Human 1.5 21.8
## 6 Owen Lars 178 120 male Human 1.78 37.9
## 7 Beru Whitesun Lars 165 75 female Human 1.65 27.5
## 8 R5-D4 97 32 none Droid 0.97 34.0
## 9 Biggs Darklighter 183 84 male Human 1.83 25.1
## 10 Obi-Wan Kenobi 182 77 male Human 1.82 23.2
## # ℹ 77 more rows
starwars_sex_sorted <- starwars_selected %>%
arrange(sex) %>%
mutate(
height_category = if_else(height > 180, "Tall","Short"))
starwars_sex_sorted
## # A tibble: 87 × 6
## name height mass sex species height_category
## <chr> <int> <dbl> <chr> <chr> <chr>
## 1 Leia Organa 150 49 female Human Short
## 2 Beru Whitesun Lars 165 75 female Human Short
## 3 Mon Mothma 150 NA female Human Short
## 4 Padmé Amidala 185 45 female Human Tall
## 5 Shmi Skywalker 163 NA female Human Short
## 6 Ayla Secura 178 55 female Twi'lek Short
## 7 Adi Gallia 184 50 female Tholothian Tall
## 8 Luminara Unduli 170 56.2 female Mirialan Short
## 9 Barriss Offee 166 50 female Mirialan Short
## 10 Dormé 165 NA female Human Short
## # ℹ 77 more rows
starwars_species_sorted <- starwars_selected %>%
arrange(species) %>%
mutate(
mass_category = if_else(mass > 80, "Heavy","Light"))
starwars_species_sorted
## # A tibble: 87 × 6
## name height mass sex species mass_category
## <chr> <int> <dbl> <chr> <chr> <chr>
## 1 Ratts Tyerel 79 15 male Aleena Light
## 2 Dexter Jettster 198 102 male Besalisk Heavy
## 3 Ki-Adi-Mundi 198 82 male Cerean Heavy
## 4 Mas Amedda 196 NA male Chagrian <NA>
## 5 Zam Wesell 168 55 female Clawdite Light
## 6 C-3PO 167 75 none Droid Light
## 7 R2-D2 96 32 none Droid Light
## 8 R5-D4 97 32 none Droid Light
## 9 IG-88 200 140 none Droid Heavy
## 10 R4-P17 96 NA none Droid <NA>
## # ℹ 77 more rows