In this table variable labels are indented within each group and a bold-formatted caption is shown.
compareGroups
package from CRAN and then load it by typing:install.packages("compareGroups")
library(compareGroups)
compareGroups
package:data(predimed)
compareGroups
function.In this example describes for each variable are computed by intervention group. Note the use of formula
argument as usual in R
, so “.” indicates all variables in the dataset. Type group
on the left side of ~
symbol indicating that descriptives will be calculated by groups.
By the argument method
we set wtn
and p14
varible to be reported as median and quartiles instead of mean and standard deviation. Note the use of subset
argument to select men or women, and the use of update
method to repeat the instruction but now selecting women instead of men.
res <- compareGroups(group ~ . , data = predimed, method = c(wtn = 2, p14 = 2))
creaTable
functionand passing the previous object computed by compareGroups
function (res
). Using this function you can customize how categorical variables are displayed (only percertage or absolute frequencies or both) by type
argument or whether standard deviation appears inside brackets or separated by plus/minus symbol by sd.type
argument. Also note the use of hide.no
category which is useful to hide “no” level for those binary variables. If you only want to show “Female” category use hide
argument for sex
variable indicating which category is going to be hiden. This argument also applies to categorical variables with more than two categories. To show how many individuals have non-missing values in each described variable, set show.n
argument to TRUE
. Finally, to specify the number of decimal digits to show use digits
arguments. In this example p14
have no decimals and for hormo
only one digit.
restab <- createTable(res, digits = c(p14 = 0, hormo=1), type = 1, sd.type = 2, hide.no = "no", hide = c(sex = 'Male'), show.n=TRUE)
Use []
to select variables and use rbind
method to create groups of variables. Note the use of varinfo
to display the original variable names in order to select variables.
varinfo(restab)
--- Analyzed variable names ----
Orig varname Shown varname
1 group Intervention group
2 sex Sex
3 age Age
4 smoke Smoking
5 bmi Body mass index
6 waist Waist circumference
7 wth Waist-to-height ratio
8 htn Hypertension
9 diab Type-2 diabetes
10 hyperchol Dyslipidemia
11 famhist Family history of premature CHD
12 hormo Hormone-replacement therapy
13 p14 MeDiet Adherence score
14 toevent follow-up to main event (years)
15 event AMI, stroke, or CV Death
# Demographic
restab1 <- restab[c('sex','age')]
# Risk factors
restab2 <- restab[c('smoke','diab','hyperchol','htn','famhist')]
# Antropomorphic
restab3 <- restab[c('bmi','waist','wth')]
# Target
restab4 <- restab[c('p14','event')]
# join tables
restab <- rbind("Demographic variables:" = restab1,
"Risk factors:" = restab2,
"Antropomorphic variables:" = restab3,
"Target variables:" = restab4)
The bivariate table can be printed in the R
console using the method print
, i.e. just typing the name of the object:
restab
--------Summary descriptives table by 'Intervention group'---------
_________________________________________________________________________________________
Control MedDiet + Nuts MedDiet + VOO p.overall N
N=2042 N=2100 N=2182
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
Demographic variables::
Sex: Female 60.2% 53.9% 58.8% <0.001 6324
Age 67.3±6.28 66.7±6.02 67.0±6.21 0.003 6324
Risk factors::
Smoking: 0.444 6324
Never 62.8% 60.0% 61.9%
Current 13.2% 14.1% 13.4%
Former 24.0% 26.0% 24.7%
Type-2 diabetes 47.5% 45.2% 49.6% 0.017 6324
Dyslipidemia 72.4% 73.3% 71.5% 0.423 6324
Hypertension 83.8% 82.8% 81.9% 0.249 6324
Family history of premature CHD 22.6% 21.9% 23.2% 0.581 6324
Antropomorphic variables::
Body mass index 30.3±3.96 29.7±3.77 29.9±3.71 <0.001 6324
Waist circumference 101±10.8 100±10.6 100±10.4 0.045 6324
Waist-to-height ratio 0.63±0.07 0.62±0.06 0.63±0.06 <0.001 6324
Target variables::
MeDiet Adherence score 8 [7;10] 9 [8;10] 9 [8;10] <0.001 6324
AMI, stroke, or CV Death 4.75% 3.33% 3.90% 0.064 6324
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
Or if you want to change some bivariate table header, such “p-value” instead of “p.overall” use header.labels
argument:
print(restab, header.labels = c(p.overall = "p-value"))
--------Summary descriptives table by 'Intervention group'---------
_______________________________________________________________________________________
Control MedDiet + Nuts MedDiet + VOO p-value N
N=2042 N=2100 N=2182
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
Demographic variables::
Sex: Female 60.2% 53.9% 58.8% <0.001 6324
Age 67.3±6.28 66.7±6.02 67.0±6.21 0.003 6324
Risk factors::
Smoking: 0.444 6324
Never 62.8% 60.0% 61.9%
Current 13.2% 14.1% 13.4%
Former 24.0% 26.0% 24.7%
Type-2 diabetes 47.5% 45.2% 49.6% 0.017 6324
Dyslipidemia 72.4% 73.3% 71.5% 0.423 6324
Hypertension 83.8% 82.8% 81.9% 0.249 6324
Family history of premature CHD 22.6% 21.9% 23.2% 0.581 6324
Antropomorphic variables::
Body mass index 30.3±3.96 29.7±3.77 29.9±3.71 <0.001 6324
Waist circumference 101±10.8 100±10.6 100±10.4 0.045 6324
Waist-to-height ratio 0.63±0.07 0.62±0.06 0.63±0.06 <0.001 6324
Target variables::
MeDiet Adherence score 8 [7;10] 9 [8;10] 9 [8;10] <0.001 6324
AMI, stroke, or CV Death 4.75% 3.33% 3.90% 0.064 6324
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
Or if you want to create a PDF document with the table in a publish-ready format.
export2pdf(restab, file = "example5.pdf", header.labels = c(p.overall = "p-value"))
To create tables in PDF you must have some LaTeX compiler installed on your computer.