You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

128 lines
3.2KB

  1. ---
  2. title: "desctable usage vignette"
  3. output: rmarkdown::html_vignette
  4. vignette: >
  5. %\VignetteIndexEntry{desctable usage vignette}
  6. %\VignetteEngine{knitr::rmarkdown}
  7. %\VignetteEncoding{UTF-8}
  8. ---
  9. ```{r, echo = F, message = F, warning = F}
  10. library(desctable)
  11. options(DT.options = list(#scrollX = T,
  12. info = F,
  13. search = F,
  14. dom = "Brtip",
  15. fixedColumns = T))
  16. knitr::opts_chunk$set(message = F, warning = F, screenshot.force = F)
  17. ```
  18. Desctable aims to be a simple and expressive interface to building statistical tables in R.
  19. # Descriptive tables
  20. ## Simple
  21. Creating a descriptive table with desctable is as easy as
  22. ```{r}
  23. iris %>%
  24. desc_table()
  25. ```
  26. <br>
  27. By default, `desc_table` will select the most appropriate statistics for the given table, but you can choose your own as easily
  28. ```{r}
  29. mtcars %>%
  30. desc_table(N = length,
  31. mean,
  32. sd)
  33. ```
  34. As you can see with `N = length`, you can give a meaningful name to the column instead of the name of the function.
  35. You are not limited in your options, and can use any statistical function that exists in R, even your own!
  36. You can also use `purrr::map`-like formulas, for example to get the first and third quartiles here
  37. ```{r}
  38. iris %>%
  39. desc_table(N = length,
  40. "%" = percent,
  41. Q1 = ~ quantile(., .25),
  42. Med = median,
  43. Q3 = ~ quantile(., .75))
  44. ```
  45. ## By group
  46. You can also create nested descriptive tables by applying `group_by` on your dataframe
  47. ```{r}
  48. iris %>%
  49. group_by(Species) %>%
  50. desc_table()
  51. ```
  52. However, because of the grouping, you can see the resulting object is not a simple data frame, but a nested dataframe (see tidyr::nest and tidyr::unnest).
  53. *desctable* provides output functions to format this object to various outputs.
  54. Right now, desctable supports `data.frame`, `pander`, and `DT` outputs. These output functions will also round numerical values, as well as p values for tests (we'll see `desc_tests` a bit later).
  55. ```{r}
  56. mtcars %>%
  57. group_by(am) %>%
  58. desc_table() %>%
  59. desc_output("df")
  60. ```
  61. ```{r}
  62. mtcars %>%
  63. group_by(am) %>%
  64. desc_table() %>%
  65. desc_output("pander")
  66. ```
  67. ```{r}
  68. mtcars %>%
  69. group_by(am) %>%
  70. desc_table() %>%
  71. desc_output("DT")
  72. ```
  73. # Comparative tables
  74. You can add tests to a grouped descriptive desctable
  75. ```{r}
  76. iris %>%
  77. group_by(Petal.Length > 5) %>%
  78. desc_table() %>%
  79. desc_tests() %>%
  80. desc_output("DT")
  81. ```
  82. By default, `desc_tests` will select the most appropriate statistical tests for the given table, but you can choose your own as easily.
  83. For example, to compare Sepal.Width using a Student's t test
  84. ```{r}
  85. iris %>%
  86. group_by(Petal.Length > 5) %>%
  87. desc_table(mean, sd, median, IQR) %>%
  88. desc_tests(Sepal.Width = ~t.test) %>%
  89. desc_output("DT")
  90. ```
  91. Note that the name of the test **must** be prepended with a tilde (`~`) in all cases!
  92. You can also use `purrr::map`-like formulas to change tests options
  93. ```{r}
  94. iris %>%
  95. group_by(Petal.Length > 5) %>%
  96. desc_table(mean, sd, median, IQR) %>%
  97. desc_tests(Sepal.Width = ~t.test(., var.equal = T)) %>%
  98. desc_output("DT")
  99. ```
  100. See the [tips and tricks](tips.html) to go further.