Preparação

Você vai precisar executar o tidyverse para este notebook.

## ── Attaching packages ──────────────────────────────── tidyverse 1.3.0 ──
## ✓ ggplot2 3.3.0     ✓ purrr   0.3.3
## ✓ tibble  2.1.3     ✓ dplyr   0.8.5
## ✓ tidyr   1.0.2     ✓ stringr 1.4.0
## ✓ readr   1.3.1     ✓ forcats 0.5.0
## ── Conflicts ─────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()

Estamos utilizando o dataset starwars quem já vem incluso no tidyverse.

starwars

Funções uteis

Glimpse: Mostra um resumo do dataset

glimpse(starwars)
## Observations: 87
## Variables: 13
## $ name       <chr> "Luke Skywalker", "C-3PO", "R2-D2", "Darth Vader", "Leia O…
## $ 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, 77…
## $ 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, "m…
## $ homeworld  <chr> "Tatooine", "Tatooine", "Naboo", "Tatooine", "Alderaan", "…
## $ species    <chr> "Human", "Droid", "Droid", "Human", "Human", "Human", "Hum…
## $ films      <list> [<"Revenge of the Sith", "Return of the Jedi", "The Empir…
## $ vehicles   <list> [<"Snowspeeder", "Imperial Speeder Bike">, <>, <>, <>, "I…
## $ starships  <list> [<"X-wing", "Imperial shuttle">, <>, <>, "TIE Advanced x1…

Filter: Filtra linhas da tabela sobre alguma condição passada como argumento.

filter(starwars, species == "Droid", skin_color == "gold")
# NROW: number of rows
NROW(starwars)
## [1] 87
# Pipe: Pass the data as the first argument of a function
starwars %>% filter(species == "Human") %>% NROW()
## [1] 35
# Comparation using pipes
starwars %>% filter(species == "Human") %>% NROW() > starwars %>% filter(species == "Droid") %>% NROW()
## [1] TRUE
# Attribution
robos_olhos_vermelhos = starwars %>% filter(species == "Droid", eye_color == "red")
robos_olhos_vermelhos
# Existe alguma pessoa careca que não é robo? Não
starwars %>% 
  filter(species != "Droid", hair_color == "NA") %>% 
  NROW()
## [1] 0
# Count: Count all ocurrences
starwars %>% 
  count(species)
# Distinct: List all distinct data
starwars %>% 
  distinct(species)
# Select: Select collumns from a dataset
starwars %>% 
  select(species, hair_color)

GGPLOT

starwars %>% 
  count(species) %>% 
  ggplot(mapping = aes(x = n,y = species)) +
  geom_point()

starwars %>% 
  group_by(species) %>% 
  summarise(count = n())