Skip to content

Instantly share code, notes, and snippets.

@avallecam
Last active January 20, 2020 21:28
Show Gist options
  • Save avallecam/9a5859bf4fe665be2da97c8c8b3ebb75 to your computer and use it in GitHub Desktop.
Save avallecam/9a5859bf4fe665be2da97c8c8b3ebb75 to your computer and use it in GitHub Desktop.
Use janitor::tabyl() para crear y adornar tablas de una, dos y tres dimensiones!
``` r
# references:
# - https://cran.r-project.org/web/packages/janitor/vignettes/janitor.html#tabyl---a-better-version-of-table
#import packages
library(dplyr)
library(janitor)
#explore dataset
#storms %>% as_tibble()
#storms %>% summary()
#storms %>% skimr::skim()
#start creating tabs!
storms %>%
#one-way quick-count
janitor::tabyl(status)
#> status n percent
#> hurricane 3091 0.3087912
#> tropical depression 2545 0.2542458
#> tropical storm 4374 0.4369630
storms %>%
#one-way quick-count
tabyl(status) %>%
#adorn output
adorn_totals("row") %>%
adorn_pct_formatting()
#> status n percent
#> hurricane 3091 30.9%
#> tropical depression 2545 25.4%
#> tropical storm 4374 43.7%
#> Total 10010 100.0%
storms %>%
#filter one variable
filter(year %in% 2013:2015) %>%
#two-way quick-count
tabyl(status,year) %>%
#adorn output
adorn_percentages("col") %>%
adorn_pct_formatting(digits = 2) %>%
adorn_ns()
#> status 2013 2014 2015
#> hurricane 6.44% (13) 53.96% (75) 22.73% (50)
#> tropical depression 23.27% (47) 17.27% (24) 26.36% (58)
#> tropical storm 70.30% (142) 28.78% (40) 50.91% (112)
storms %>%
#filter two variable
filter(year %in% 2013:2015,
month %in% 8:10) %>%
#three-way quick-count
tabyl(status,year,month) %>%
#adorn output
adorn_percentages("col") %>%
adorn_pct_formatting(digits = 2) %>%
adorn_ns()
#> $`10`
#> status 2013 2014 2015
#> hurricane 0.00% (0) 59.26% (32) 93.33% (28)
#> tropical depression 13.16% (5) 16.67% (9) 0.00% (0)
#> tropical storm 86.84% (33) 24.07% (13) 6.67% (2)
#>
#> $`8`
#> status 2013 2014 2015
#> hurricane 0.00% (0) 60.87% (14) 28.00% (14)
#> tropical depression 30.43% (7) 8.70% (2) 6.00% (3)
#> tropical storm 69.57% (16) 30.43% (7) 66.00% (33)
#>
#> $`9`
#> status 2013 2014 2015
#> hurricane 16.25% (13) 54.55% (18) 4.17% (4)
#> tropical depression 28.75% (23) 6.06% (2) 42.71% (41)
#> tropical storm 55.00% (44) 39.39% (13) 53.12% (51)
```
<sup>Created on 2020-01-20 by the [reprex package](https://reprex.tidyverse.org) (v0.3.0)</sup>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment