README.md

reportTools

Functions to help writing report in RMarkdown.

About

How to use

There are three functions, described below. Each takes a data.frame and returns a table.

table_of_counts

table_of_counts takes a data.frame and an optional grouping variable and returns a flextable table giving the N, percent, and range for each variable.

data(mtcars)

mtcars %>%
  select(cyl, vs) %>%
  table_of_counts()

Variable

Category

N (%)

Range

cyl

4

11 (34.4)

7-14

6

7 (21.9)

7-14

8

14 (43.8)

7-14

vs

0

18 (56.2)

14-18

1

14 (43.8)

14-18

With a grouping variable:

mtcars %>%
  select(cyl, vs, am) %>%
  table_of_counts(cyl)

Variable

Category

4

6

8

Total

am

0

3 (15.8)

4 (21.1)

12 (63.2)

19

1

8 (61.5)

3 (23.1)

2 (15.4)

13

vs

0

1 (5.6)

3 (16.7)

14 (77.8)

18

1

10 (71.4)

4 (28.6)

14

You can, of course, use this without the pipe (%>%), but it's less convenient:

table_of_counts(mtcars[, c("cyl", "vs", "am")])

table_of_means

table_of_means takes a data.frame, a comma-separated list of variables, and an optional grouping variable. It returns a data.frame with the mean (SD) [range] for each variable.

# Load data
data(starwars)

Without a grouping variable:

table_of_means(starwars, 
               height, mass, birth_year)

# A tibble: 3 x 3
  Variable   female              male                 
  <chr>      <chr>               <chr>                
1 birth_year 47.2 (15) [19-72]   84.8 (154.8) [8-896] 
2 height     165.5 (23) [96-213] 179.2 (35.4) [66-264]
3 mass       54 (8.4) [45-75]    81 (28.2) [15-159] 

With a grouping variable:

table_of_means(starwars,
               height, mass, birth_year,
               group = gender) 

# A tibble: 3 x 3
  Variable   female              male                 
  <chr>      <chr>               <chr>                
1 birth_year 47.2 (15) [19-72]   84.8 (154.8) [8-896] 
2 height     165.5 (23) [96-213] 179.2 (35.4) [66-264]
3 mass       54 (8.4) [45-75]    81 (28.2) [15-159]   

The first argument is the source data frame, the last argument is the optional grouping variable. This function is pipeable, in which case the first argument (data) isn't needed:

starwars %>%
  table_of_means(height, mass, birth_year)

The resulting data.frame can be used in an RMarkdown document with pandoc.table (from the pander package):

library(pander)
starwars %>%
  table_of_means(height, mass, birth_year) %>%
  pandoc.table(caption = "Summary statistics",
               justify  = "lr")

Note that pandoc.table makes it easy to set the caption and cell alignment (justify).

table_of_medians

The table_of_medians function is identical to the previous, except that it returns the median (IQR) [range] for each variable.

starwars %>%
  table_of_medians(height, mass, birth_year) %>%
  pandoc.table(caption = "Summary statistics",
               justify  = "lr")

-----------------------------------
Variable       Median (IQR) [range]
------------ ----------------------
birth_year          52 (37) [8-896]

height            180 (24) [66-264]

mass            79 (28.9) [15-1358]
-----------------------------------

Table: Summary statistics

Tips

  1. All functions supported labelled data. If you import labelled data using haven from SPSS or Stata, variable and value labels will be used.


ewancarr/report-tools documentation built on May 10, 2019, 10:01 a.m.