Building tables using the carpenter R package
In biomedical research, there are certain types of tables that are often included in the article. For instance, some basic statistics between the treatment and control group. Or maybe it is between males and females, before and after an intervention, and so on. Often these tables are a hassle to create and are prone to needing updates based on slight changes in the data or from reviewer comments. I can’t stand doing these repetitive and simple tasks by hand, so I designed carpenter to make creating these tables easily.
Using carpenter and building a table
The underlying design principal for carpenter is that you create an outline of what the table should look like before finally creating the table. This is how carpenters also work: they sketch what the product will look like before actually starting to build anything. There are four ‘outlining’ functions, several carpenter statistics functions, and one final ‘building’ function:
outline_table()starts the outlining process.
add_rows()adds variables to the row or rows with associated descriptive statistics.
stat_*()type statistic functions to be used with
add_rows()(a list of available statistics can be found using
renaming()for customizing the naming of the rows and table headers.
build-table()for finally building the table (uses the
These functions are chained together using the wonderful
%>% pipe. If you’ve
never used this package or the pipe, take a look at the vignette on introducing
it. So, let’s do some coding:
You’ll notice that the
outline_table function returned a
tibble of 0 rows
and 0 columns. That’s because we haven’t added anything else to the table!
carpenter is waiting for more information. So we add rows by:
You see it has now added a row to the table. Adding more rows:
See how this can make creating these tables very easy. Let’s remove the
the row names and fix up the table header names:
And finally build the table into a Markdown format for easy insertion into R Markdown documents:
|Sepal Length||5.0 (0.4)||5.9 (0.5)||6.6 (0.6)|
|Petal Length||1.5 (0.2)||4.3 (0.5)||5.6 (0.6)|
|Sepal Width||3.4 (3.2-3.7)||2.8 (2.5-3.0)||3.0 (2.8-3.2)|
If you have factor/discrete data, you can even use include these variables:
|Miles/gallon||16.6 (3.9)||24.6 (5.4)|
|Read axle ratio||3.2 (3.1-3.7)||3.9 (3.7-4.1)|
|Number of gears|
|- 3||12 (66.7%)||3 (21.4%)|
|- 4||2 (11.1%)||10 (71.4%)|
|- 5||4 (22.2%)||1 (7.1%)|
Pretty easy eh?
Sometimes, though, you don’t need to compare multiple columns, but instead need
to only show one column. Easy, just don’t include a header in the
Note: This is a slightly modified version of the introduction vignette for carpenter (
vignette('carpenter')from version 0.2.0). Presently, carpenter only creates tables that you would typically see as the ‘basic characteristics’ or ‘descriptive statistics’ table in most biomedical articles. The plan is to include other common table structures, but they haven’t been implemented yet. If you have any suggestions, let me know as an Issue!