Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.08 KB | None | 0 0
  1. # Crosstabs
  2.  
  3. Sometimes you want your results in a crosstab. We can use the `tabyl` function in `janitor` package to make crosstabs automatically.
  4.  
  5. Create a crosstab of `gender` and `health_gen`.
  6.  
  7.  
  8. ```{r}
  9. nhanes %>%
  10. tabyl(gender, health_gen)
  11. ```
  12.  
  13. Add a `drop_na` before your line with `tabyl` to get rid of all NAs.
  14.  
  15. ```{r}
  16. nhanes %>%
  17. drop_na(gender, health_gen) %>%
  18. tabyl(gender, health_gen)
  19. ```
  20.  
  21.  
  22. `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`.
  23.  
  24. Use the code above and then add totals using `adorn_totals` in the rows and columns.
  25.  
  26. ```{r}
  27. nhanes %>%
  28. drop_na(gender, health_gen) %>%
  29. tabyl(gender, health_gen) %>%
  30. adorn_totals(where = c("row", "col"))
  31. ```
  32.  
  33.  
  34. We can add `adorn_percentages` to add percentages.
  35.  
  36. Use the code above and then add percentages using `adorn_percentages`.
  37.  
  38.  
  39. ```{r}
  40. nhanes %>%
  41. drop_na(gender, health_gen) %>%
  42. tabyl(gender, health_gen) %>%
  43. adorn_totals(where = c("row", "col")) %>%
  44. adorn_percentages()
  45. ```
  46.  
  47.  
  48.  
  49. We can then format these percentages using `adorn_pct_formatting`.
  50.  
  51. 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).
  52.  
  53. ```{r}
  54. nhanes %>%
  55. drop_na(gender, health_gen) %>%
  56. tabyl(gender, health_gen) %>%
  57. adorn_totals(where = c("row", "col")) %>%
  58. adorn_percentages() %>%
  59. adorn_pct_formatting(digits = 1,
  60. rounding = "half up")
  61. ```
  62.  
  63.  
  64.  
  65. If we want to include the n alongside percentages, we can use `adorn_ns`.
  66.  
  67. Use the code above and then add a line with `adorn_ns` to include the n.
  68.  
  69. ```{r}
  70. nhanes %>%
  71. drop_na(gender, health_gen) %>%
  72. tabyl(gender, health_gen) %>%
  73. adorn_totals(c("row", "col")) %>%
  74. adorn_percentages() %>%
  75. adorn_pct_formatting(digits = 1,
  76. rounding = "half up") %>%
  77. adorn_ns()
  78. ```
  79.  
  80.  
  81.  
  82. We can add titles to our crosstabs using `adorn_title`.
  83.  
  84. Use the code above and then add a title using `adorn_title`. Use the `placement` argument and see what you get.
  85.  
  86. ```{r}
  87. nhanes %>%
  88. drop_na(gender, health_gen) %>%
  89. tabyl(gender, health_gen) %>%
  90. adorn_totals(c("row", "col")) %>%
  91. adorn_percentages() %>%
  92. adorn_pct_formatting(digits = 0,
  93. rounding = "half up") %>%
  94. adorn_ns() %>%
  95. adorn_title(placement = "combined")
  96. ```
  97.  
  98.  
  99.  
  100.  
  101. We can also do three (or more) way crosstabs automatically by adding more variables to the `tabyl` function.
  102.  
  103. 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.
  104.  
  105. ```{r}
  106. nhanes %>%
  107. drop_na(gender, health_gen, age_decade) %>%
  108. tabyl(gender, health_gen, age_decade) %>%
  109. adorn_totals(c("row", "col")) %>%
  110. adorn_percentages() %>%
  111. adorn_pct_formatting(digits = 0, rounding = "half up") %>%
  112. adorn_ns() %>%
  113. adorn_title()
  114.  
  115. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement