Skip to contents

Generate a statistics table with the chosen statistical functions, and tests if given a "grouped" dataframe.

Usage

desctable(data, stats, tests, labels)

# S3 method for default
desctable(data, stats = stats_auto, tests, labels = NULL)

# S3 method for grouped_df
desctable(data, stats = stats_auto, tests = tests_auto, labels = NULL)

Arguments

data

The dataframe to analyze

stats

A list of named statistics to apply to each element of the dataframe, or a function returning a list of named statistics

tests

A list of statistical tests to use when calling desctable with a grouped_df

labels

A named character vector of labels to use instead of variable names

Value

A desctable object, which prints to a table of statistics for all variables

Labels

labels is an option named character vector used to make the table prettier.

If given, the variable names for which there is a label will be replaced by their corresponding label.

Not all variables need to have a label, and labels for non-existing variables are ignored.

labels must be given in the form c(unquoted_variable_name = "label")

Stats

The stats can be a function which takes a dataframe and returns a list of statistical functions to use.

stats can also be a named list of statistical functions, or purrr::map like 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.

Tests

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.

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), or a purrr::map like formula (e.g. ~t.test(., var.equal = T)). 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.

If data is a grouped dataframe (using group_by), subtables are created and statistic tests are performed over each sub-group.

Output

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 are present. Printing reduces the object to a dataframe.

Examples

iris %>%
  desctable()
#> Warning: desctable is deprecated and will be removed in 1.0.0.
#> 
#> Please use the `desc_*` family of functions (`desc_table`, `desc_tests`, `desc_output`)
#>                         N        % Min  Q1  Med     Mean  Q3 Max        sd IQR
#> 1        Sepal.Length 150       NA 4.3 5.1 5.80 5.843333 6.4 7.9 0.8280661 1.3
#> 2         Sepal.Width 150       NA 2.0 2.8 3.00 3.057333 3.3 4.4 0.4358663 0.5
#> 3        Petal.Length 150       NA 1.0 1.6 4.35 3.758000 5.1 6.9 1.7652982 3.5
#> 4         Petal.Width 150       NA 0.1 0.3 1.30 1.199333 1.8 2.5 0.7622377 1.5
#> 5             Species 150       NA  NA  NA   NA       NA  NA  NA        NA  NA
#> 6     Species: setosa  50 33.33333  NA  NA   NA       NA  NA  NA        NA  NA
#> 7 Species: versicolor  50 33.33333  NA  NA   NA       NA  NA  NA        NA  NA
#> 8  Species: virginica  50 33.33333  NA  NA   NA       NA  NA  NA        NA  NA

# Does the same as stats_auto here
iris %>%
  desctable(stats = list("N"      = length,
                         "Mean"   = ~ if (is.normal(.)) mean(.),
                         "sd"     = ~ if (is.normal(.)) sd(.),
                         "Med"    = stats::median,
                         "IQR"    = ~ if(!is.factor(.)) IQR(.)))
#> Warning: desctable is deprecated and will be removed in 1.0.0.
#> 
#> Please use the `desc_*` family of functions (`desc_table`, `desc_tests`, `desc_output`)
#>                         N     Mean        sd  Med IQR
#> 1        Sepal.Length 150       NA        NA 5.80 1.3
#> 2         Sepal.Width 150 3.057333 0.4358663 3.00 0.5
#> 3        Petal.Length 150       NA        NA 4.35 3.5
#> 4         Petal.Width 150       NA        NA 1.30 1.5
#> 5             Species 150       NA        NA   NA  NA
#> 6     Species: setosa  50       NA        NA   NA  NA
#> 7 Species: versicolor  50       NA        NA   NA  NA
#> 8  Species: virginica  50       NA        NA   NA  NA

# With labels
mtcars %>% desctable(labels = c(hp  = "Horse Power",
                                cyl = "Cylinders",
                                mpg = "Miles per gallon"))
#> Warning: desctable is deprecated and will be removed in 1.0.0.
#> 
#> Please use the `desc_*` family of functions (`desc_table`, `desc_tests`, `desc_output`)
#>                        Min        Q1     Med       Mean     Q3     Max
#> 1  Miles per gallon 10.400  15.42500  19.200  20.090625  22.80  33.900
#> 2         Cylinders  4.000   4.00000   6.000   6.187500   8.00   8.000
#> 3              disp 71.100 120.82500 196.300 230.721875 326.00 472.000
#> 4       Horse Power 52.000  96.50000 123.000 146.687500 180.00 335.000
#> 5              drat  2.760   3.08000   3.695   3.596563   3.92   4.930
#> 6                wt  1.513   2.58125   3.325   3.217250   3.61   5.424
#> 7              qsec 14.500  16.89250  17.710  17.848750  18.90  22.900
#> 8                vs  0.000   0.00000   0.000   0.437500   1.00   1.000
#> 9                am  0.000   0.00000   0.000   0.406250   1.00   1.000
#> 10             gear  3.000   3.00000   4.000   3.687500   4.00   5.000
#> 11             carb  1.000   2.00000   2.000   2.812500   4.00   8.000
#>             sd       IQR
#> 1    6.0269481   7.37500
#> 2    1.7859216   4.00000
#> 3  123.9386938 205.17500
#> 4   68.5628685  83.50000
#> 5    0.5346787   0.84000
#> 6    0.9784574   1.02875
#> 7    1.7869432   2.00750
#> 8    0.5040161   1.00000
#> 9    0.4989909   1.00000
#> 10   0.7378041   1.00000
#> 11   1.6152000   2.00000

# With grouping on a factor
iris %>%
  group_by(Species) %>%
  desctable(stats = stats_default)
#> Warning: desctable is deprecated and will be removed in 1.0.0.
#> 
#> Please use the `desc_*` family of functions (`desc_table`, `desc_tests`, `desc_output`)
#>                Species: setosa (n=50) / N Species: setosa (n=50) / %
#> 1 Sepal.Length                         50                         NA
#> 2  Sepal.Width                         50                         NA
#> 3 Petal.Length                         50                         NA
#> 4  Petal.Width                         50                         NA
#>   Species: setosa (n=50) / Mean Species: setosa (n=50) / sd
#> 1                         5.006                   0.3524897
#> 2                         3.428                   0.3790644
#> 3                            NA                          NA
#> 4                            NA                          NA
#>   Species: setosa (n=50) / Med Species: setosa (n=50) / IQR
#> 1                          5.0                        0.400
#> 2                          3.4                        0.475
#> 3                          1.5                        0.175
#> 4                          0.2                        0.100
#>   Species: versicolor (n=50) / N Species: versicolor (n=50) / %
#> 1                             50                             NA
#> 2                             50                             NA
#> 3                             50                             NA
#> 4                             50                             NA
#>   Species: versicolor (n=50) / Mean Species: versicolor (n=50) / sd
#> 1                             5.936                       0.5161711
#> 2                             2.770                       0.3137983
#> 3                             4.260                       0.4699110
#> 4                                NA                              NA
#>   Species: versicolor (n=50) / Med Species: versicolor (n=50) / IQR
#> 1                             5.90                            0.700
#> 2                             2.80                            0.475
#> 3                             4.35                            0.600
#> 4                             1.30                            0.300
#>   Species: virginica (n=50) / N Species: virginica (n=50) / %
#> 1                            50                            NA
#> 2                            50                            NA
#> 3                            50                            NA
#> 4                            50                            NA
#>   Species: virginica (n=50) / Mean Species: virginica (n=50) / sd
#> 1                            6.588                      0.6358796
#> 2                            2.974                      0.3224966
#> 3                            5.552                      0.5518947
#> 4                               NA                             NA
#>   Species: virginica (n=50) / Med Species: virginica (n=50) / IQR    tests / p
#> 1                            6.50                           0.675 8.918734e-22
#> 2                            3.00                           0.375 1.569282e-14
#> 3                            5.55                           0.775 4.803974e-29
#> 4                            2.00                           0.500 3.261796e-29
#>   tests / test
#> 1 kruskal.test
#> 2 kruskal.test
#> 3 kruskal.test
#> 4 kruskal.test

# With nested grouping, on arbitrary variables
mtcars %>%
  group_by(vs, cyl) %>%
  desctable()
#> Warning: desctable is deprecated and will be removed in 1.0.0.
#> 
#> Please use the `desc_*` family of functions (`desc_table`, `desc_tests`, `desc_output`)
#> Warning: cannot compute exact p-value with ties
#> Warning: cannot compute exact p-value with ties
#> Warning: cannot compute exact p-value with ties
#> Warning: cannot compute exact p-value with ties
#> Warning: cannot compute exact p-value with ties
#> Warning: cannot compute exact p-value with ties
#> Warning: cannot compute exact p-value with ties
#> Warning: cannot compute exact p-value with ties
#> Warning: cannot compute exact p-value with ties
#>        vs: 0 (n=18) / cyl: 4 (n=1) / Min vs: 0 (n=18) /  / Q1
#> 1  mpg                             26.00                26.00
#> 2 disp                            120.30               120.30
#> 3   hp                             91.00                91.00
#> 4 drat                              4.43                 4.43
#> 5   wt                              2.14                 2.14
#> 6 qsec                             16.70                16.70
#> 7   am                              1.00                 1.00
#> 8 gear                              5.00                 5.00
#> 9 carb                              2.00                 2.00
#>   vs: 0 (n=18) /  / Med vs: 0 (n=18) /  / Mean vs: 0 (n=18) /  / Q3
#> 1                 26.00                  26.00                26.00
#> 2                120.30                 120.30               120.30
#> 3                 91.00                  91.00                91.00
#> 4                  4.43                   4.43                 4.43
#> 5                  2.14                   2.14                 2.14
#> 6                 16.70                  16.70                16.70
#> 7                  1.00                   1.00                 1.00
#> 8                  5.00                   5.00                 5.00
#> 9                  2.00                   2.00                 2.00
#>   vs: 0 (n=18) /  / Max vs: 0 (n=18) /  / sd vs: 0 (n=18) /  / IQR
#> 1                 26.00                   NA                     0
#> 2                120.30                   NA                     0
#> 3                 91.00                   NA                     0
#> 4                  4.43                   NA                     0
#> 5                  2.14                   NA                     0
#> 6                 16.70                   NA                     0
#> 7                  1.00                   NA                     0
#> 8                  5.00                   NA                     0
#> 9                  2.00                   NA                     0
#>   vs: 0 (n=18) / cyl: 6 (n=3) / Min vs: 0 (n=18) /  / Q1 vs: 0 (n=18) /  / Med
#> 1                             19.70               20.350                 21.00
#> 2                            145.00              152.500                160.00
#> 3                            110.00              110.000                110.00
#> 4                              3.62                3.760                  3.90
#> 5                              2.62                2.695                  2.77
#> 6                             15.50               15.980                 16.46
#> 7                              1.00                1.000                  1.00
#> 8                              4.00                4.000                  4.00
#> 9                              4.00                4.000                  4.00
#>   vs: 0 (n=18) /  / Mean vs: 0 (n=18) /  / Q3 vs: 0 (n=18) /  / Max
#> 1              20.566667              21.0000                21.000
#> 2             155.000000             160.0000               160.000
#> 3             131.666667             142.5000               175.000
#> 4               3.806667               3.9000                 3.900
#> 5               2.755000               2.8225                 2.875
#> 6              16.326667              16.7400                17.020
#> 7               1.000000               1.0000                 1.000
#> 8               4.333333               4.5000                 5.000
#> 9               4.666667               5.0000                 6.000
#>   vs: 0 (n=18) /  / sd vs: 0 (n=18) /  / IQR vs: 0 (n=18) / cyl: 8 (n=14) / Min
#> 1            0.7505553                0.6500                              10.40
#> 2            8.6602540                7.5000                             275.80
#> 3           37.5277675               32.5000                             150.00
#> 4            0.1616581                0.1400                               2.76
#> 5            0.1281601                0.1275                               3.17
#> 6            0.7687219                0.7600                              14.50
#> 7            0.0000000                0.0000                               0.00
#> 8            0.5773503                0.5000                               3.00
#> 9            1.1547005                1.0000                               2.00
#>   vs: 0 (n=18) /  / Q1 vs: 0 (n=18) /  / Med vs: 0 (n=18) /  / Mean
#> 1              14.4000                15.200             15.1000000
#> 2             301.7500               350.500            353.1000000
#> 3             176.2500               192.500            209.2142857
#> 4               3.0700                 3.115              3.2292857
#> 5               3.5325                 3.755              3.9992143
#> 6              16.0975                17.175             16.7721429
#> 7               0.0000                 0.000              0.1428571
#> 8               3.0000                 3.000              3.2857143
#> 9               2.2500                 3.500              3.5000000
#>   vs: 0 (n=18) /  / Q3 vs: 0 (n=18) /  / Max vs: 0 (n=18) /  / sd
#> 1             16.25000                19.200            2.5600481
#> 2            390.00000               472.000           67.7713236
#> 3            241.25000               335.000           50.9768855
#> 4              3.22500                 4.220            0.3723618
#> 5              4.01375                 5.424            0.7594047
#> 6             17.55500                18.000            1.1960138
#> 7              0.00000                 1.000            0.3631365
#> 8              3.00000                 5.000            0.7262730
#> 9              4.00000                 8.000            1.5566236
#>   vs: 0 (n=18) /  / IQR vs: 0 (n=18) / tests / p vs: 0 (n=18) /  / test
#> 1               1.85000               0.01124786           kruskal.test
#> 2              88.25000               0.01109171           kruskal.test
#> 3              65.00000               0.02510507           kruskal.test
#> 4               0.15500               0.03286104           kruskal.test
#> 5               0.48125               0.01135263           kruskal.test
#> 6               1.45750               0.47244498           kruskal.test
#> 7               0.00000               0.00777266           kruskal.test
#> 8               0.00000               0.01810659           kruskal.test
#> 9               1.75000               0.12211556           kruskal.test
#>   vs: 1 (n=14) / cyl: 4 (n=10) / Min vs: 1 (n=14) /  / Q1 vs: 1 (n=14) /  / Med
#> 1                             21.400              22.8000                25.850
#> 2                             71.100              78.7750               101.550
#> 3                             52.000              65.2500                79.500
#> 4                              3.690               3.7900                 4.000
#> 5                              1.513               1.8600                 2.260
#> 6                             16.900              18.6025                19.185
#> 7                              0.000               0.2500                 1.000
#> 8                              3.000               4.0000                 4.000
#> 9                              1.000               1.0000                 1.500
#>   vs: 1 (n=14) /  / Mean vs: 1 (n=14) /  / Q3 vs: 1 (n=14) /  / Max
#> 1                26.7300             30.40000                 33.90
#> 2               103.6200            120.77500                146.70
#> 3                81.8000             96.50000                113.00
#> 4                 4.0350              4.10250                  4.93
#> 5                 2.3003              2.70125                  3.19
#> 6                19.3810             19.97500                 22.90
#> 7                 0.7000              1.00000                  1.00
#> 8                 4.0000              4.00000                  5.00
#> 9                 1.5000              2.00000                  2.00
#>   vs: 1 (n=14) /  / sd vs: 1 (n=14) /  / IQR vs: 1 (n=14) / cyl: 6 (n=4) / Min
#> 1            4.7481107               7.60000                            17.800
#> 2           27.8246414              42.00000                           167.600
#> 3           21.8723570              31.25000                           105.000
#> 4            0.3642115               0.31250                             2.760
#> 5            0.5982073               0.84125                             3.215
#> 6            1.5553453               1.37250                            18.300
#> 7            0.4830459               0.75000                             0.000
#> 8            0.4714045               0.00000                             3.000
#> 9            0.5270463               1.00000                             1.000
#>   vs: 1 (n=14) /  / Q1 vs: 1 (n=14) /  / Med vs: 1 (n=14) /  / Mean
#> 1             18.02500                 18.65               19.12500
#> 2            167.60000                196.30              204.55000
#> 3            108.75000                116.50              115.25000
#> 4              3.00000                  3.50                3.42000
#> 5              3.38375                  3.44                3.38875
#> 6             18.75000                 19.17               19.21500
#> 7              0.00000                  0.00                0.00000
#> 8              3.00000                  3.50                3.50000
#> 9              1.00000                  2.50                2.50000
#>   vs: 1 (n=14) /  / Q3 vs: 1 (n=14) /  / Max vs: 1 (n=14) /  / sd
#> 1               19.750                 21.40            1.6317169
#> 2              233.250                258.00           44.7426344
#> 3              123.000                123.00            9.1787799
#> 4                3.920                  3.92            0.5919459
#> 5                3.445                  3.46            0.1162164
#> 6               19.635                 20.22            0.8159044
#> 7                0.000                  0.00            0.0000000
#> 8                4.000                  4.00            0.5773503
#> 9                4.000                  4.00            1.7320508
#>   vs: 1 (n=14) /  / IQR vs: 1 (n=14) / tests / p vs: 1 (n=14) /  / test
#> 1               1.72500              0.007019704            wilcox.test
#> 2              65.65000              0.005766846            wilcox.test
#> 3              14.25000              0.019356258            wilcox.test
#> 4               0.92000              0.135397657            wilcox.test
#> 5               0.06125              0.005766846            wilcox.test
#> 6               0.88500              1.000000000            wilcox.test
#> 7               0.00000              0.027884340            wilcox.test
#> 8               1.00000              0.129468688            wilcox.test
#> 9               3.00000              0.485044622            wilcox.test

# With grouping on a condition, and choice of tests
iris %>%
  group_by(Petal.Length > 5) %>%
  desctable(tests = list(.auto = tests_auto, Species = ~chisq.test))
#> Warning: desctable is deprecated and will be removed in 1.0.0.
#> 
#> Please use the `desc_*` family of functions (`desc_table`, `desc_tests`, `desc_output`)
#>                       Petal.Length > 5: FALSE (n=108) / N
#> 1        Sepal.Length                                 108
#> 2         Sepal.Width                                 108
#> 3        Petal.Length                                 108
#> 4         Petal.Width                                 108
#> 5             Species                                 108
#> 6     Species: setosa                                  50
#> 7 Species: versicolor                                  49
#> 8  Species: virginica                                   9
#>   Petal.Length > 5: FALSE (n=108) / % Petal.Length > 5: FALSE (n=108) / Min
#> 1                                  NA                                   4.3
#> 2                                  NA                                   2.0
#> 3                                  NA                                   1.0
#> 4                                  NA                                   0.1
#> 5                                  NA                                    NA
#> 6                           46.296296                                    NA
#> 7                           45.370370                                    NA
#> 8                            8.333333                                    NA
#>   Petal.Length > 5: FALSE (n=108) / Q1 Petal.Length > 5: FALSE (n=108) / Med
#> 1                                  5.0                                   5.5
#> 2                                  2.8                                   3.0
#> 3                                  1.5                                   3.5
#> 4                                  0.2                                   1.0
#> 5                                   NA                                    NA
#> 6                                   NA                                    NA
#> 7                                   NA                                    NA
#> 8                                   NA                                    NA
#>   Petal.Length > 5: FALSE (n=108) / Mean Petal.Length > 5: FALSE (n=108) / Q3
#> 1                              5.5018519                                  6.0
#> 2                              3.0666667                                  3.4
#> 3                              3.0074074                                  4.5
#> 4                              0.8638889                                  1.4
#> 5                                     NA                                   NA
#> 6                                     NA                                   NA
#> 7                                     NA                                   NA
#> 8                                     NA                                   NA
#>   Petal.Length > 5: FALSE (n=108) / Max Petal.Length > 5: FALSE (n=108) / sd
#> 1                                   7.0                            0.6386290
#> 2                                   4.4                            0.4800701
#> 3                                   5.0                            1.4885673
#> 4                                   2.0                            0.6110292
#> 5                                    NA                                   NA
#> 6                                    NA                                   NA
#> 7                                    NA                                   NA
#> 8                                    NA                                   NA
#>   Petal.Length > 5: FALSE (n=108) / IQR Petal.Length > 5: TRUE (n=42) / N
#> 1                                   1.0                                42
#> 2                                   0.6                                42
#> 3                                   3.0                                42
#> 4                                   1.2                                42
#> 5                                    NA                                42
#> 6                                    NA                                 0
#> 7                                    NA                                 1
#> 8                                    NA                                41
#>   Petal.Length > 5: TRUE (n=42) / % Petal.Length > 5: TRUE (n=42) / Min
#> 1                                NA                                 5.8
#> 2                                NA                                 2.5
#> 3                                NA                                 5.1
#> 4                                NA                                 1.4
#> 5                                NA                                  NA
#> 6                          0.000000                                  NA
#> 7                          2.380952                                  NA
#> 8                         97.619048                                  NA
#>   Petal.Length > 5: TRUE (n=42) / Q1 Petal.Length > 5: TRUE (n=42) / Med
#> 1                              6.325                                 6.7
#> 2                              2.800                                 3.0
#> 3                              5.300                                 5.6
#> 4                              1.825                                 2.1
#> 5                                 NA                                  NA
#> 6                                 NA                                  NA
#> 7                                 NA                                  NA
#> 8                                 NA                                  NA
#>   Petal.Length > 5: TRUE (n=42) / Mean Petal.Length > 5: TRUE (n=42) / Q3
#> 1                             6.721429                              7.175
#> 2                             3.033333                              3.200
#> 3                             5.688095                              5.975
#> 4                             2.061905                              2.300
#> 5                                   NA                                 NA
#> 6                                   NA                                 NA
#> 7                                   NA                                 NA
#> 8                                   NA                                 NA
#>   Petal.Length > 5: TRUE (n=42) / Max Petal.Length > 5: TRUE (n=42) / sd
#> 1                                 7.9                          0.5748958
#> 2                                 3.8                          0.2968671
#> 3                                 6.9                          0.4919857
#> 4                                 2.5                          0.2802023
#> 5                                  NA                                 NA
#> 6                                  NA                                 NA
#> 7                                  NA                                 NA
#> 8                                  NA                                 NA
#>   Petal.Length > 5: TRUE (n=42) / IQR    tests / p tests / test
#> 1                               0.850 1.553676e-15  wilcox.test
#> 2                               0.400 6.927432e-01  wilcox.test
#> 3                               0.675 2.076978e-21  wilcox.test
#> 4                               0.475 1.577769e-19  wilcox.test
#> 5                                  NA 2.707889e-24   chisq.test
#> 6                                  NA           NA         <NA>
#> 7                                  NA           NA         <NA>
#> 8                                  NA           NA         <NA>