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.

111 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, labels = NULL)
  11. \method{desctable}{grouped_df}(data, stats = stats_auto, tests = tests_auto, labels = NULL)
  12. }
  13. \arguments{
  14. \item{data}{The dataframe to analyze}
  15. \item{stats}{A list of named statistics to apply to each element of the dataframe, or a function returning a list of named statistics}
  16. \item{tests}{A list of statistical tests to use when calling desctable with a grouped_df}
  17. \item{labels}{A named character vector of labels to use instead of variable names}
  18. }
  19. \value{
  20. A desctable object, which prints to a table of statistics for all variables
  21. }
  22. \description{
  23. Generate a statistics table with the chosen statistical functions, and tests if given a \code{"grouped"} dataframe.
  24. }
  25. \section{Labels}{
  26. labels is an option named character vector used to make the table prettier.
  27. If given, the variable names for which there is a label will be replaced by their corresponding label.
  28. Not all variables need to have a label, and labels for non-existing variables are ignored.
  29. labels must be given in the form c(unquoted_variable_name = "label")
  30. }
  31. \section{Stats}{
  32. The stats can be a function which takes a dataframe and returns a list of statistical functions to use.
  33. stats can also be a named list of statistical functions, or formulas.
  34. 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.
  35. The general form is \code{condition ~ T | F}, and can be nested, such as \code{is.factor ~ percent | (is.normal ~ mean | median)}, for example.
  36. }
  37. \section{Tests}{
  38. 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.
  39. 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.
  40. 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}.
  41. If data is a grouped dataframe (using \code{group_by}), subtables are created and statistic tests are performed over each sub-group.
  42. }
  43. \section{Output}{
  44. 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.
  45. }
  46. \examples{
  47. iris \%>\%
  48. desctable()
  49. # Does the same as stats_auto here
  50. iris \%>\%
  51. desctable(stats = list("N" = length,
  52. "\%/Mean" = is.factor ~ percent | (is.normal ~ mean),
  53. "sd" = is.normal ~ sd,
  54. "Med" = is.normal ~ NA | median,
  55. "IQR" = is.normal ~ NA | IQR))
  56. # With labels
  57. mtcars \%>\% desctable(labels = c(hp = "Horse Power",
  58. cyl = "Cylinders",
  59. mpg = "Miles per gallon"))
  60. # With grouping on a factor
  61. iris \%>\%
  62. group_by(Species) \%>\%
  63. desctable(stats = stats_default)
  64. # With nested grouping, on arbitrary variables
  65. mtcars \%>\%
  66. group_by(vs, cyl) \%>\%
  67. desctable()
  68. # With grouping on a condition, and choice of tests
  69. iris \%>\%
  70. group_by(Petal.Length > 5) \%>\%
  71. desctable(tests = list(.auto = tests_auto, Species = ~chisq.test))
  72. }
  73. \seealso{
  74. \code{\link{stats_auto}}
  75. \code{\link{tests_auto}}
  76. \code{\link{print.desctable}}
  77. \code{\link{pander.desctable}}
  78. \code{\link{datatable.desctable}}
  79. }