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.

92 lines
3.8KB

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