tbl_num_sum()
is a wrapper around mosaic::favstats()
and creates a tidy table of summary statistics
including the min, q1, median, q3, max, mean, standard deviation, n, and missing values.
Arguments
- data
A data frame (or tibble).
- formula
Variables to summarize given in formula notation:
~var1
orvar1~var2
.var1
should be numeric andvar2
, when supplied, should be a grouping variable.- digits
The number if digits to round to. Defaults to 3.
An override for the table caption. A sensible default is given.
- na_rm
Should missing values be removed? Defaults to TRUE.
Value
An object of class flextable. If in an interactive session, the table will be viewable immediately.
Examples
tbl_num_sum(mtcars, ~wt)
Summary Statistics for wt
NAs Removed: Yes min
Q1
median
Q3
max
mean
sd
n
missing
1.513
2.581
3.325
3.610
5.424
3.217
0.978
32
0
tbl_num_sum(mtcars, ~wt)
Summary Statistics for wt
NAs Removed: Yes min
Q1
median
Q3
max
mean
sd
n
missing
1.513
2.581
3.325
3.610
5.424
3.217
0.978
32
0
tbl_num_sum(mtcars, ~wt, digits = 2, caption = "This is a table")
This is a table
NAs Removed: Yes min
Q1
median
Q3
max
mean
sd
n
missing
1.51
2.58
3.33
3.61
5.42
3.22
0.98
32
0
tbl_num_sum(mtcars, wt~cyl)
Summary Statistics By Group: wt by cyl
NAs Removed: Yes cyl
min
Q1
median
Q3
max
mean
sd
n
missing
4
1.513
1.885
2.200
2.622
3.190
2.286
0.570
11
0
6
2.620
2.822
3.215
3.440
3.460
3.117
0.356
7
0
8
3.170
3.533
3.755
4.014
5.424
3.999
0.759
14
0
# not removing NAs is an option but not recommended
tbl_num_sum(airquality, ~Ozone, na_rm = FALSE)
#> ! NAs were detected but not removed. You may get missing values in your output.
Summary Statistics for Ozone
NAs Removed: No min
Q1
median
Q3
max
mean
sd
n
missing
NA
NA
NA
NA
NA
NA
NA
116
37
tbl_num_sum(airquality, Ozone~Month, na_rm = FALSE)
#> ! NAs were detected but not removed. You may get missing values in your output.
Summary Statistics By Group: Ozone by Month
NAs Removed: No Month
min
Q1
median
Q3
max
mean
sd
n
missing
5
NA
NA
NA
NA
NA
NA
NA
26
5
6
NA
NA
NA
NA
NA
NA
NA
9
21
7
NA
NA
NA
NA
NA
NA
NA
26
5
8
NA
NA
NA
NA
NA
NA
NA
26
5
9
NA
NA
NA
NA
NA
NA
NA
29
1