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.

113 lines
3.9KB

  1. % Generated by roxygen2: do not edit by hand
  2. % Please edit documentation in R/build.R
  3. \name{desctable}
  4. \alias{desctable}
  5. \alias{desctable.default}
  6. \alias{desctable.grouped_df}
  7. \title{Generate a statistics table}
  8. \usage{
  9. desctable(data, stats, tests, labels)
  10. \method{desctable}{default}(data, stats = stats_auto, tests,
  11. labels = NULL)
  12. \method{desctable}{grouped_df}(data, stats = stats_auto,
  13. tests = tests_auto, labels = NULL)
  14. }
  15. \arguments{
  16. \item{data}{The dataframe to analyze}
  17. \item{stats}{A list of named statistics to apply to each element of the dataframe, or a function returning a list of named statistics}
  18. \item{tests}{A list of statistical tests to use when calling desctable with a grouped_df}
  19. \item{labels}{A named character vector of labels to use instead of variable names}
  20. }
  21. \value{
  22. A desctable object, which prints to a table of statistics for all variables
  23. }
  24. \description{
  25. Generate a statistics table with the chosen statistical functions, and tests if given a \code{"grouped"} dataframe.
  26. }
  27. \section{Labels}{
  28. labels is an option named character vector used to make the table prettier.
  29. If given, the variable names for which there is a label will be replaced by their corresponding label.
  30. Not all variables need to have a label, and labels for non-existing variables are ignored.
  31. labels must be given in the form c(unquoted_variable_name = "label")
  32. }
  33. \section{Stats}{
  34. The stats can be a function which takes a dataframe and returns a list of statistical functions to use.
  35. stats can also be a named list of statistical functions, or formulas.
  36. The names will be used as column names in the resulting table. If an element of the list is a function, it will be used as-is for the stats. If an element of the list is a formula, it can be used to conditionally use stats depending on the variable.
  37. The general form is \code{condition ~ T | F}, and can be nested, such as \code{is.factor ~ percent | (is.normal ~ mean | median)}, for example.
  38. }
  39. \section{Tests}{
  40. The tests can be a function which takes a variable and a grouping variable, and returns an appropriate statistical test to use in that case.
  41. tests can also be a named list of statistical test functions, associating the name of a variable in the data, and a test to use specifically for that variable.
  42. That test name must be expressed as a single-term formula (e.g. \code{~t.test}). You don't have to specify tests for all the variables: a default test for all other variables can be defined with the name \code{.default}, and an automatic test can be defined with the name \code{.auto}.
  43. If data is a grouped dataframe (using \code{group_by}), subtables are created and statistic tests are performed over each sub-group.
  44. }
  45. \section{Output}{
  46. The output is a desctable object, which is a list of named dataframes that can be further manipulated. Methods for printing, using in \pkg{pander} and \pkg{DT} are present. Printing reduces the object to a dataframe.
  47. }
  48. \examples{
  49. iris \%>\%
  50. desctable
  51. # Does the same as stats_auto here
  52. iris \%>\%
  53. desctable(stats = list("N" = length,
  54. "\%/Mean" = is.factor ~ percent | (is.normal ~ mean),
  55. "sd" = is.normal ~ sd,
  56. "Med" = is.normal ~ NA | median,
  57. "IQR" = is.normal ~ NA | IQR))
  58. # With labels
  59. mtcars \%>\% desctable(labels = c(hp = "Horse Power",
  60. cyl = "Cylinders",
  61. mpg = "Miles per gallon"))
  62. # With grouping on a factor
  63. iris \%>\%
  64. group_by(Species) \%>\%
  65. desctable(stats = stats_default)
  66. # With nested grouping, on arbitrary variables
  67. mtcars \%>\%
  68. group_by(vs, cyl) \%>\%
  69. desctable
  70. # With grouping on a condition, and choice of tests
  71. iris \%>\%
  72. group_by(Petal.Length > 5) \%>\%
  73. desctable(tests = list(.auto = tests_auto, Species = ~chisq.test))
  74. }
  75. \seealso{
  76. \code{\link{stats_auto}}
  77. \code{\link{tests_auto}}
  78. \code{\link{print.desctable}}
  79. \code{\link{pander.desctable}}
  80. \code{\link{datatable.desctable}}
  81. }