Skip to contents

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.

Usage

tbl_num_sum(data, formula, digits = 3, caption = NULL, na_rm = FALSE)

Arguments

data

A data frame (or tibble).

formula

Variables to summarize given in formula notation: ~var1 or var1~var2. var1 should be numeric and var2, when supplied, should be a grouping variable.

digits

The number if digits to round to. Defaults to 3.

caption

An override for the table caption. A sensible default is given.

na_rm

Should missing values be removed? Defaults to FALSE.

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: No

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, na_rm = TRUE)
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, na_rm = TRUE, 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, na_rm = TRUE)
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 not recommended tbl_num_sum(airquality, ~Ozone) #> ! 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) #> ! 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

# easy fix tbl_num_sum(airquality, Ozone~Month, na_rm = TRUE)
Summary Statistics By Group: Ozone by Month
NAs Removed: Yes

Month

min

Q1

median

Q3

max

mean

sd

n

missing

5

1.000

11.000

18.000

31.500

115.000

23.615

22.224

26

5

6

12.000

20.000

23.000

37.000

71.000

29.444

18.208

9

21

7

7.000

36.250

60.000

79.750

135.000

59.115

31.636

26

5

8

9.000

28.750

52.000

82.500

168.000

59.962

39.681

26

5

9

7.000

16.000

23.000

36.000

96.000

31.448

24.142

29

1