```{r setup}
#| warning: false
#| message: false
library(palmerpenguins)
library(tidyverse)
library(gt)
```
É possível usar a função pivot_wider com mais de uma coluna.
```{r}
#| warning: false
#| message: false
#| fig-cap: "Palmer Penguins - primeira versao"
#| label: palmer1
palmerpenguins::penguins %>%
count(island, year, sex,species) %>%
gt()
```
island | year | sex | species | n |
---|---|---|---|---|
Biscoe | 2007 | female | Adelie | 5 |
Biscoe | 2007 | female | Gentoo | 16 |
Biscoe | 2007 | male | Adelie | 5 |
Biscoe | 2007 | male | Gentoo | 17 |
Biscoe | 2007 | NA | Gentoo | 1 |
Biscoe | 2008 | female | Adelie | 9 |
Biscoe | 2008 | female | Gentoo | 22 |
Biscoe | 2008 | male | Adelie | 9 |
Biscoe | 2008 | male | Gentoo | 23 |
Biscoe | 2008 | NA | Gentoo | 1 |
Biscoe | 2009 | female | Adelie | 8 |
Biscoe | 2009 | female | Gentoo | 20 |
Biscoe | 2009 | male | Adelie | 8 |
Biscoe | 2009 | male | Gentoo | 21 |
Biscoe | 2009 | NA | Gentoo | 3 |
Dream | 2007 | female | Adelie | 9 |
Dream | 2007 | female | Chinstrap | 13 |
Dream | 2007 | male | Adelie | 10 |
Dream | 2007 | male | Chinstrap | 13 |
Dream | 2007 | NA | Adelie | 1 |
Dream | 2008 | female | Adelie | 8 |
Dream | 2008 | female | Chinstrap | 9 |
Dream | 2008 | male | Adelie | 8 |
Dream | 2008 | male | Chinstrap | 9 |
Dream | 2009 | female | Adelie | 10 |
Dream | 2009 | female | Chinstrap | 12 |
Dream | 2009 | male | Adelie | 10 |
Dream | 2009 | male | Chinstrap | 12 |
Torgersen | 2007 | female | Adelie | 8 |
Torgersen | 2007 | male | Adelie | 7 |
Torgersen | 2007 | NA | Adelie | 5 |
Torgersen | 2008 | female | Adelie | 8 |
Torgersen | 2008 | male | Adelie | 8 |
Torgersen | 2009 | female | Adelie | 8 |
Torgersen | 2009 | male | Adelie | 8 |
Palmer Penguins - primeira versao
Vamos fazer pivot_wider com as colunas species e sex. Queremos que o resultado tenha as colunas com nomes como “Adele (female)”.
```{r}
#| warning: false
#| message: false
#| fig-cap: "Quantidade de pinguins por espécie e sexo"
#| label: palmer2
penguins %>%
count(island, year, species, sex) %>%
filter(!is.na(sex)) %>%
pivot_wider(names_from = c(species, sex),
values_from = n,
names_glue = "{species} ({sex})",
values_fill = 0) %>%
gt()
```
island | year | Adelie (female) | Adelie (male) | Gentoo (female) | Gentoo (male) | Chinstrap (female) | Chinstrap (male) |
---|---|---|---|---|---|---|---|
Biscoe | 2007 | 5 | 5 | 16 | 17 | 0 | 0 |
Biscoe | 2008 | 9 | 9 | 22 | 23 | 0 | 0 |
Biscoe | 2009 | 8 | 8 | 20 | 21 | 0 | 0 |
Dream | 2007 | 9 | 10 | 0 | 0 | 13 | 13 |
Dream | 2008 | 8 | 8 | 0 | 0 | 9 | 9 |
Dream | 2009 | 10 | 10 | 0 | 0 | 12 | 12 |
Torgersen | 2007 | 8 | 7 | 0 | 0 | 0 | 0 |
Torgersen | 2008 | 8 | 8 | 0 | 0 | 0 | 0 |
Torgersen | 2009 | 8 | 8 | 0 | 0 | 0 | 0 |
Quantidade de pinguins por espécie e sexo
Ainda pode melhorar, veja como formatar tabelas com o pacote gt neste [post](https://abreums.github.io/posts/2022-11-05-r-tip-criando-tabelas-com-o-pacote-gt/).
Citation
BibTeX citation:
@misc{abreu2022,
author = {Abreu, Marcos},
title = {R tip: como fazer pivot\_wider com mais de uma coluna},
date = {2022-11-05},
url = {https://abreums.github.io/posts/2022-11-05-r-tip-pivot-wider-com-mais-de-uma-coluna/},
langid = {pt-br}
}
For attribution, please cite this work as:
Abreu, Marcos. 2022. “R tip: como fazer pivot_wider com mais de
uma coluna.” November 5, 2022. https://abreums.github.io/posts/2022-11-05-r-tip-pivot-wider-com-mais-de-uma-coluna/.