library(tidyverse)
## ── 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()
O dataset utilizado é o Starwars que vem incluso na biblioteca tidyverse.
starwars
A função glimpse(x) mostra um resumo dos dados passados como parâmetro:
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…
A função filter(x) faz um filtro sobre a condição passada como parâmetro:
filter(starwars, species == "Droid", skin_color == "gold")
A função NROW(x) mostra a quantidade de dados da condição passada como parâmetro:
NROW(starwars)
## [1] 87
A função count(x) retorna a um inteiro referente a quantidade de dados da condição passada como parâmetro:
starwars %>%
count(species)
A função group_by(x) agrupa os dados de acordo com a variável passada como parâmetro.
A função summarise(x) sumariza os dados de acordo com a variável passada como parâmetro.
starwars %>%
group_by(species) %>%
summarise(count = n())
As especies mais comuns:
contagem = starwars %>%
count(species, sort = TRUE)
contagem
starwars %>%
filter(species == "Human", homeworld == "Tatooine") %>%
select(name, birth_year)
contagem %>%
filter(n > 1) %>%
ggplot(mapping = aes(x = n, y = species)) + geom_point()
# + geom_col
starwars %>%
count(species) %>%
ggplot(mapping = aes(x = n,y = species)) +
geom_point()