Atividade 2
Exercícios propostos em Exercício - Semana 2
Dados:
egg <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2023/2023-04-11/egg-production.csv')
Carregar pacote:
Visualizar a tabela dos dados:
library(kableExtra)
#| tbl-cap: "Tabela 1. Dados dos Ovos"
#| echo: false
#| cap-location: top
kable(head(egg))
| 2016-07-31 |
hatching eggs |
all |
57975000 |
1147000000 |
ChicEggs-09-23-2016.pdf |
| 2016-08-31 |
hatching eggs |
all |
57595000 |
1142700000 |
ChicEggs-10-21-2016.pdf |
| 2016-09-30 |
hatching eggs |
all |
57161000 |
1093300000 |
ChicEggs-11-22-2016.pdf |
| 2016-10-31 |
hatching eggs |
all |
56857000 |
1126700000 |
ChicEggs-12-23-2016.pdf |
| 2016-11-30 |
hatching eggs |
all |
57116000 |
1096600000 |
ChicEggs-01-24-2017.pdf |
| 2016-12-31 |
hatching eggs |
all |
57750000 |
1132900000 |
ChicEggs-02-28-2017.pdf |
Exercícios
- Remover a última coluna do dataset (a coluna chamada source).
egg.1 <- select(egg, -source)
kable(head(egg.1))
| 2016-07-31 |
hatching eggs |
all |
57975000 |
1147000000 |
| 2016-08-31 |
hatching eggs |
all |
57595000 |
1142700000 |
| 2016-09-30 |
hatching eggs |
all |
57161000 |
1093300000 |
| 2016-10-31 |
hatching eggs |
all |
56857000 |
1126700000 |
| 2016-11-30 |
hatching eggs |
all |
57116000 |
1096600000 |
| 2016-12-31 |
hatching eggs |
all |
57750000 |
1132900000 |
- Selecionar apenas as variáveis numéricas.
num.egg <- select_if(egg.1, is.numeric)
kable(head(num.egg))
| 57975000 |
1147000000 |
| 57595000 |
1142700000 |
| 57161000 |
1093300000 |
| 56857000 |
1126700000 |
| 57116000 |
1096600000 |
| 57750000 |
1132900000 |
- Criar um dataset só com hatching e outro dataset apenas com table eggs.
hat.<- filter(egg, prod_type == "hatching eggs")
table.eggs<- filter(egg, prod_type=="table eggs")
kable(head(table.eggs))
| 2016-07-31 |
table eggs |
all |
299669000 |
7350500000 |
ChicEggs-09-23-2016.pdf |
| 2016-08-31 |
table eggs |
all |
300917000 |
7409000000 |
ChicEggs-10-21-2016.pdf |
| 2016-09-30 |
table eggs |
all |
303270000 |
7204200000 |
ChicEggs-11-22-2016.pdf |
| 2016-10-31 |
table eggs |
all |
305852000 |
7534700000 |
ChicEggs-12-23-2016.pdf |
| 2016-11-30 |
table eggs |
all |
310728000 |
7468500000 |
ChicEggs-01-24-2017.pdf |
| 2016-12-31 |
table eggs |
all |
318820000 |
7950400000 |
ChicEggs-02-28-2017.pdf |
- Criar um dataset só com table eggs e todos os processos (all).
table.all <- filter(egg, prod_type=="table eggs", prod_process=="all")
#outra opção
table.all2 <- filter(table.eggs, prod_process=="all")
kable(head(table.all))
| 2016-07-31 |
table eggs |
all |
299669000 |
7350500000 |
ChicEggs-09-23-2016.pdf |
| 2016-08-31 |
table eggs |
all |
300917000 |
7409000000 |
ChicEggs-10-21-2016.pdf |
| 2016-09-30 |
table eggs |
all |
303270000 |
7204200000 |
ChicEggs-11-22-2016.pdf |
| 2016-10-31 |
table eggs |
all |
305852000 |
7534700000 |
ChicEggs-12-23-2016.pdf |
| 2016-11-30 |
table eggs |
all |
310728000 |
7468500000 |
ChicEggs-01-24-2017.pdf |
| 2016-12-31 |
table eggs |
all |
318820000 |
7950400000 |
ChicEggs-02-28-2017.pdf |
- Mudar os nomes das
variáveis para português.
ovos <- rename(egg, mês_observado=observed_month, tipo_produto=prod_type, prod_processado=prod_process, n_galinhas=n_hens, n_ovos=n_eggs, origem=source)
kable(head(ovos))
| 2016-07-31 |
hatching eggs |
all |
57975000 |
1147000000 |
ChicEggs-09-23-2016.pdf |
| 2016-08-31 |
hatching eggs |
all |
57595000 |
1142700000 |
ChicEggs-10-21-2016.pdf |
| 2016-09-30 |
hatching eggs |
all |
57161000 |
1093300000 |
ChicEggs-11-22-2016.pdf |
| 2016-10-31 |
hatching eggs |
all |
56857000 |
1126700000 |
ChicEggs-12-23-2016.pdf |
| 2016-11-30 |
hatching eggs |
all |
57116000 |
1096600000 |
ChicEggs-01-24-2017.pdf |
| 2016-12-31 |
hatching eggs |
all |
57750000 |
1132900000 |
ChicEggs-02-28-2017.pdf |
- Mudar os nomes dos
fatores para português.
ovos2 <- mutate(ovos, tipo_produto=recode(tipo_produto, `hatching eggs`= "ovos_para_chocar", `table eggs`= "ovos_de_mesa"),
prod_processado=recode(prod_processado, all="todos", `cage-free (non-organic)`= "livres-não orgânico", `cage-free (organic)`= "livres-orgânico"))
kable(head(ovos2))
| 2016-07-31 |
ovos_para_chocar |
todos |
57975000 |
1147000000 |
ChicEggs-09-23-2016.pdf |
| 2016-08-31 |
ovos_para_chocar |
todos |
57595000 |
1142700000 |
ChicEggs-10-21-2016.pdf |
| 2016-09-30 |
ovos_para_chocar |
todos |
57161000 |
1093300000 |
ChicEggs-11-22-2016.pdf |
| 2016-10-31 |
ovos_para_chocar |
todos |
56857000 |
1126700000 |
ChicEggs-12-23-2016.pdf |
| 2016-11-30 |
ovos_para_chocar |
todos |
57116000 |
1096600000 |
ChicEggs-01-24-2017.pdf |
| 2016-12-31 |
ovos_para_chocar |
todos |
57750000 |
1132900000 |
ChicEggs-02-28-2017.pdf |
- Criar uma nova variável chamada ‘produtividade’ com a razão entre número de ovos (n_eggs) e número de galinhas (n_hens).
ovos3<- mutate(ovos2, Produtividade=(`n_ovos`/`n_galinhas`))
kable(head(ovos3))
| 2016-07-31 |
ovos_para_chocar |
todos |
57975000 |
1147000000 |
ChicEggs-09-23-2016.pdf |
19.78439 |
| 2016-08-31 |
ovos_para_chocar |
todos |
57595000 |
1142700000 |
ChicEggs-10-21-2016.pdf |
19.84026 |
| 2016-09-30 |
ovos_para_chocar |
todos |
57161000 |
1093300000 |
ChicEggs-11-22-2016.pdf |
19.12668 |
| 2016-10-31 |
ovos_para_chocar |
todos |
56857000 |
1126700000 |
ChicEggs-12-23-2016.pdf |
19.81638 |
| 2016-11-30 |
ovos_para_chocar |
todos |
57116000 |
1096600000 |
ChicEggs-01-24-2017.pdf |
19.19952 |
| 2016-12-31 |
ovos_para_chocar |
todos |
57750000 |
1132900000 |
ChicEggs-02-28-2017.pdf |
19.61732 |
- Criar um dataset só de produtos cage free e criar novas variáveis separando n_eggs e n_hens por orgânicos e não orgânicos.
sep.1 <- ovos2 %>%
filter(prod_processado %in% c("livres-não orgânico", "livres-orgânico")) %>%
separate(prod_processado, into = c("processo", "tipo"), sep = "-") %>%
pivot_wider(names_from = tipo, values_from = c(n_galinhas, n_ovos))
kable(head(sep.1))
| 2016-08-31 |
ovos_de_mesa |
livres |
PY20160919MCAGEFREE.PDF |
17000000 |
13500000 |
397884291 |
315968297 |
| 2016-09-30 |
ovos_de_mesa |
livres |
PY20161004MCAGEFREE.PDF |
17000000 |
13500000 |
383774914 |
304762114 |
| 2016-10-31 |
ovos_de_mesa |
livres |
PY20161102MCAGEFREE.PDF |
23500000 |
14100000 |
546374469 |
327825000 |
| 2016-11-30 |
ovos_de_mesa |
livres |
PY20161205MCAGEFREE.PDF |
23500000 |
14100000 |
530864743 |
318519771 |
| 2016-12-31 |
ovos_de_mesa |
livres |
PY20170109MCAGEFREE.PDF |
23500000 |
14100000 |
542733120 |
325639234 |
| 2017-01-31 |
ovos_de_mesa |
livres |
PY20170206MCAGEFREE.PDF |
23500000 |
14100000 |
550017411 |
330010766 |
- Criar uma variável categórica com os fatores n_eggs e n_hens, combinando seus valores em uma única coluna.
juntar <- ovos2 %>%
pivot_longer(cols = c(n_galinhas, n_ovos), names_to = "tipo", values_to = "valor")
kable(head(juntar))
| 2016-07-31 |
ovos_para_chocar |
todos |
ChicEggs-09-23-2016.pdf |
n_galinhas |
57975000 |
| 2016-07-31 |
ovos_para_chocar |
todos |
ChicEggs-09-23-2016.pdf |
n_ovos |
1147000000 |
| 2016-08-31 |
ovos_para_chocar |
todos |
ChicEggs-10-21-2016.pdf |
n_galinhas |
57595000 |
| 2016-08-31 |
ovos_para_chocar |
todos |
ChicEggs-10-21-2016.pdf |
n_ovos |
1142700000 |
| 2016-09-30 |
ovos_para_chocar |
todos |
ChicEggs-11-22-2016.pdf |
n_galinhas |
57161000 |
| 2016-09-30 |
ovos_para_chocar |
todos |
ChicEggs-11-22-2016.pdf |
n_ovos |
1093300000 |
- Faça um sumário dos dados com as médias de n_hens e n_eggs por ano, por produto e por processo.
sum<- ovos3 %>%
separate(mês_observado, into = c("ano", "mês", "dia"), sep ="-") %>%
select(-mês, -dia, -origem) %>%
group_by(ano, tipo_produto, prod_processado) %>%
summarise_all(mean)
kable(head(sum))
| 2016 |
ovos_de_mesa |
livres-não orgânico |
20900000 |
480326307 |
22.98299 |
| 2016 |
ovos_de_mesa |
livres-orgânico |
13860000 |
318542883 |
22.98301 |
| 2016 |
ovos_de_mesa |
todos |
306542667 |
7486216667 |
24.41880 |
| 2016 |
ovos_para_chocar |
todos |
57409000 |
1123200000 |
19.56409 |
| 2017 |
ovos_de_mesa |
livres-não orgânico |
29358333 |
672053117 |
22.87501 |
| 2017 |
ovos_de_mesa |
livres-orgânico |
14466667 |
330960497 |
22.87500 |