Skip to content
This repository has been archived by the owner on Nov 22, 2023. It is now read-only.

Commit

Permalink
termina cap 12
Browse files Browse the repository at this point in the history
  • Loading branch information
beamilz-ensino committed Oct 17, 2020
1 parent a0b2b53 commit 2c4a7a8
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 48 deletions.
104 changes: 58 additions & 46 deletions 12-tidy-data.Rmd
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
# 12 - Tidy data
# 12 Tidy data

## 12.1 Introduction
[Confira o livro neste link](https://r4ds.had.co.nz/tidy-data.html)

Sessão sem códigos.


### 12.1.1 Prerequisites
### Prerequisites (12.1.1)

```{r message=FALSE, warning=FALSE}
# library(tidyverse)
Expand All @@ -16,10 +13,9 @@ library(dplyr)
library(tidyr)
```

## 12.2 Tidy data
## Tidy data (12.2)

```{r}
tabela1
tabela2
Expand All @@ -28,43 +24,42 @@ tabela3
# Spread across two tibbles
tabela4a # cases
tabela4a # casos
tabela4b # population
tabela4b # população
```

```{r}
# Compute rate per 10,000
# Calcule a taxa por 10.000 pessoas
tabela1 %>%
mutate(taxa = casos / populacao * 10000)
```

```{r}
# Compute cases per year
# Calcule casos por ano
tabela1 %>%
count(ano, wt = casos)
```

```{r}
library(ggplot2)
ggplot(tabela1, aes(ano, casos)) +
geom_line(aes(group = pais), colour = "grey50") +
geom_point(aes(colour = pais))
```

## 12.3 Pivoting
## Pivoting (12.3)

### 12.3.1 Longer
### Longer (12.3.1)


```{r}
tabela4a
```

```{r}
tabela4a %>%
tabela4a %>%
pivot_longer(c(`1999`, `2000`), names_to = "ano", values_to = "casos")
```

Expand All @@ -86,7 +81,7 @@ left_join(tidy4a, tidy4b)
```


### 12.3.2 Wider
### Wider (12.3.2)

```{r}
tabela2
Expand All @@ -97,9 +92,9 @@ tabela2 %>%
pivot_wider(names_from = tipo, values_from = contagem)
```

## 12.4 Separating and uniting
## Separating and uniting (12.4)

### 12.4.1 Separate
### Separate (12.4.1)

```{r}
tabela3
Expand All @@ -122,14 +117,12 @@ tabela3 %>%
```




```{r}
tabela3 %>%
separate(ano, into = c("seculo", "ano"), sep = 2)
```

### 12.4.2 Unite
### Unite (12.4.2)

```{r}
tabela5 %>%
Expand All @@ -142,25 +135,13 @@ tabela5 %>%
```



### 12.5 Missing values

Não usa bases de dados que está no pacote dados.


## 12.6 Case Study
## Case Study (12.6)

```{r}
dados_oms
```

```{r}
dados_oms <- dados::dados_oms
who <- tidyr::who
dados_oms1 <- dados_oms %>% #Problema aqui! Porque nossa base tem 1 coluna a menos?
pivot_longer(
cols = novos_fpp_h014:novos_recaida_m65,
Expand All @@ -172,20 +153,51 @@ dados_oms1
```

```{r}
who1 <- who %>%
pivot_longer(
cols = new_sp_m014:newrel_f65,
names_to = "key",
values_to = "cases",
values_drop_na = TRUE
)
who1
dados_oms1 %>%
count(chave)
```

```{r}
dados_oms2 <- dados_oms1 %>%
# Precisa substituir o newrel - qual seria o equivalente?
mutate(chave = stringr::str_replace(chave, "newrel", "new_rel"))
dados_oms2
```

```{r}
dados_oms1 %>%
count(chave)
dados_oms3 <- dados_oms2 %>%
separate(chave, c("novos", "tipo", "sexo_faixa_etaria"), sep = "_")
dados_oms3
```

```{r}
dados_oms3 %>%
count(novos)
dados_oms4 <- dados_oms3 %>%
select(-novos, -iso2, -iso3)
```

```{r}
dados_oms5 <- dados_oms4 %>%
separate(sexo_faixa_etaria, c("sexo", "faixa_etaria"), sep = 1)
dados_oms5
```

```{r}
dados_oms %>%
pivot_longer(
cols = novos_fpp_h014:novos_recaida_m65,
names_to = "chave",
values_to = "casos",
values_drop_na = TRUE
) %>%
mutate(
# Como alterar a linha abaixo?
chave = stringr::str_replace(chave, "newrel", "new_rel")
) %>%
separate(chave, c("novos", "tipo", "sexo_faixa_etaria")) %>%
select(-novos, -iso2, -iso3) %>%
separate(sexo_faixa_etaria, c("sexo", "faixa_etaria"), sep = 1)
```

[Continuar]
6 changes: 4 additions & 2 deletions _bookdown.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
book_filename: "bookdown-demo"
book_filename: "R4DS_dados"
language:
ui:
chapter_name: "Chapter "
chapter_name: "Capítulo "
delete_merged_file: true

before_chapter_script: "_common.R"
19 changes: 19 additions & 0 deletions _common.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Código de https://github.com/hadley/r4ds/blob/master/_common.R

set.seed(1014)

knitr::opts_chunk$set(
comment = "#>",
collapse = TRUE,
cache = TRUE,
out.width = "70%",
fig.align = 'center',
fig.width = 6,
fig.asp = 0.618, # 1 / phi
fig.show = "hold"
)

options(dplyr.print_min = 6, dplyr.print_max = 6)

# Supress crayon output
options(crayon.enabled = FALSE)

0 comments on commit 2c4a7a8

Please sign in to comment.