Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Crosstabs
- Sometimes you want your results in a crosstab. We can use the `tabyl` function in `janitor` package to make crosstabs automatically.
- Create a crosstab of `gender` and `health_gen`.
- ```{r}
- nhanes %>%
- tabyl(gender, health_gen)
- ```
- Add a `drop_na` before your line with `tabyl` to get rid of all NAs.
- ```{r}
- nhanes %>%
- drop_na(gender, health_gen) %>%
- tabyl(gender, health_gen)
- ```
- `janitor` has a set of functions that all start with `adorn_` that add a number of things to our crosstabs. We call them after `tabyl`. For example, `adorn_totals`.
- Use the code above and then add totals using `adorn_totals` in the rows and columns.
- ```{r}
- nhanes %>%
- drop_na(gender, health_gen) %>%
- tabyl(gender, health_gen) %>%
- adorn_totals(where = c("row", "col"))
- ```
- We can add `adorn_percentages` to add percentages.
- Use the code above and then add percentages using `adorn_percentages`.
- ```{r}
- nhanes %>%
- drop_na(gender, health_gen) %>%
- tabyl(gender, health_gen) %>%
- adorn_totals(where = c("row", "col")) %>%
- adorn_percentages()
- ```
- We can then format these percentages using `adorn_pct_formatting`.
- Use the code above and then format the percentages using `adorn_pct_formatting`. Add arguments so that the percentages are rounded to 1 digit. Note that R uses the "half to even" rounding method by default so if you want to round, say, 14.5 to 15 you must use the `rounding` argument (type ?adorn_pct_formatting in the console to learn more).
- ```{r}
- nhanes %>%
- drop_na(gender, health_gen) %>%
- tabyl(gender, health_gen) %>%
- adorn_totals(where = c("row", "col")) %>%
- adorn_percentages() %>%
- adorn_pct_formatting(digits = 1,
- rounding = "half up")
- ```
- If we want to include the n alongside percentages, we can use `adorn_ns`.
- Use the code above and then add a line with `adorn_ns` to include the n.
- ```{r}
- nhanes %>%
- drop_na(gender, health_gen) %>%
- tabyl(gender, health_gen) %>%
- adorn_totals(c("row", "col")) %>%
- adorn_percentages() %>%
- adorn_pct_formatting(digits = 1,
- rounding = "half up") %>%
- adorn_ns()
- ```
- We can add titles to our crosstabs using `adorn_title`.
- Use the code above and then add a title using `adorn_title`. Use the `placement` argument and see what you get.
- ```{r}
- nhanes %>%
- drop_na(gender, health_gen) %>%
- tabyl(gender, health_gen) %>%
- adorn_totals(c("row", "col")) %>%
- adorn_percentages() %>%
- adorn_pct_formatting(digits = 0,
- rounding = "half up") %>%
- adorn_ns() %>%
- adorn_title(placement = "combined")
- ```
- We can also do three (or more) way crosstabs automatically by adding more variables to the `tabyl` function.
- Use the code above, but add a third variable (`age_decade`) to the line with `drop_na` and the line with `tabyl`. You should get a series of crosstabs.
- ```{r}
- nhanes %>%
- drop_na(gender, health_gen, age_decade) %>%
- tabyl(gender, health_gen, age_decade) %>%
- adorn_totals(c("row", "col")) %>%
- adorn_percentages() %>%
- adorn_pct_formatting(digits = 0, rounding = "half up") %>%
- adorn_ns() %>%
- adorn_title()
- ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement