Function “mytable”” produce table for descriptive analysis easily. It is most useful to make table to describe baseline characteristics common in medical research papers.
If you are unfamiliar to package moonBook and mytable function, please see the R package moonBook
vignette at: https://CRAN.R-project.org/package=moonBook/vignettes/moonBook.html
You can use mytable
function to explore a data.frame.
require(moonBook)
require(ztable)
require(magrittr)
options(ztable.type="html")
mytable(acs)
Descriptive Statistics
———————————————————————————————————————————————————————— % N Missing (%)
Mean ± SD or
———————————————————————————————————————————————————————— 63.3 ± 11.7 857 0 ( 0.0%)
age 857 0 ( 0.0%)
sex - Female 287 (33.5%)
- Male 570 (66.5%)
857 0 ( 0.0%)
cardiogenicShock - No 805 (93.9%)
- Yes 52 (6.1%)
857 0 ( 0.0%)
entry - Femoral 312 (36.4%)
- Radial 545 (63.6%)
857 0 ( 0.0%)
Dx - NSTEMI 153 (17.9%)
- STEMI 304 (35.5%)
- Unstable Angina 400 (46.7%)
55.8 ± 9.6 723 134 (15.6%)
EF 163.2 ± 9.1 764 93 (10.9%)
height 64.8 ± 11.4 766 91 (10.6%)
weight 24.3 ± 3.3 764 93 (10.9%)
BMI 857 0 ( 0.0%)
obesity - No 567 (66.2%)
- Yes 290 (33.8%)
185.2 ± 47.8 834 23 ( 2.7%)
TC 116.6 ± 41.1 833 24 ( 2.8%)
LDLC 38.2 ± 11.1 834 23 ( 2.7%)
HDLC 125.2 ± 90.9 842 15 ( 1.8%)
TG 857 0 ( 0.0%)
DM - No 553 (64.5%)
- Yes 304 (35.5%)
857 0 ( 0.0%)
HBP - No 356 (41.5%)
- Yes 501 (58.5%)
857 0 ( 0.0%)
smoking - Ex-smoker 204 (23.8%)
- Never 332 (38.7%)
- Smoker 321 (37.5%)
————————————————————————————————————————————————————————
You can use formula without grouping variable(s).
mytable(~age+sex,data=acs)
Descriptive Statistics
————————————————————————————————————————————— % N Missing (%)
Mean ± SD or
————————————————————————————————————————————— 63.3 ± 11.7 857 0 ( 0.0%)
age 857 0 ( 0.0%)
sex - Female 287 (33.5%)
- Male 570 (66.5%)
—————————————————————————————————————————————
You can compress
mytable. If rows dealing with categorical variables have two unique values, it can be printed in a single row rather than three rows.
mytable(Dx~sex,data=acs)
'Dx'
Descriptive Statistics by
—————————————————————————————————————————————————————————
NSTEMI STEMI Unstable Angina p N=153) (N=304) (N=400)
(
————————————————————————————————————————————————————————— 0.012
sex - Female 50 (32.7%) 84 (27.6%) 153 (38.2%)
- Male 103 (67.3%) 220 (72.4%) 247 (61.8%)
—————————————————————————————————————————————————————————
mytable(Dx~sex,data=acs) %>% compress
'Dx'
Descriptive Statistics by
———————————————————————————————————————————————————————
NSTEMI STEMI Unstable Angina p N=153) (N=304) (N=400)
(
——————————————————————————————————————————————————————— :Male 103 (67.3%) 220 (72.4%) 247 (61.8%) 0.012
sex ———————————————————————————————————————————————————————
The default representative group is the second group. If you want the first group to being representative group, please use the no
argument.
mytable(Dx~sex,data=acs) %>% compress(no=1)
'Dx'
Descriptive Statistics by
———————————————————————————————————————————————————————
NSTEMI STEMI Unstable Angina p N=153) (N=304) (N=400)
(
——————————————————————————————————————————————————————— :Female 50 (32.7%) 84 (27.6%) 153 (38.2%) 0.012
sex ———————————————————————————————————————————————————————
Sometimes it is more simple to omit the representative group name. You can do this by set the add.label argument FALSE.
mytable(Dx~cardiogenicShock+DM+obesity+HBP,data=acs) %>% compress
'Dx'
Descriptive Statistics by
——————————————————————————————————————————————————————————————————
NSTEMI STEMI Unstable Angina p N=153) (N=304) (N=400)
(
—————————————————————————————————————————————————————————————————— :Yes 4 ( 2.6%) 48 (15.8%) 0 ( 0.0%) 0.000
cardiogenicShock:Yes 57 (37.3%) 96 (31.6%) 151 (37.8%) 0.209
DM:Yes 47 (30.7%) 95 (31.2%) 148 (37.0%) 0.186
obesity:Yes 91 (59.5%) 154 (50.7%) 256 (64.0%) 0.002
HBP ——————————————————————————————————————————————————————————————————
mytable(Dx~cardiogenicShock+DM+obesity+HBP,data=acs) %>% compress(add.label=FALSE)
'Dx'
Descriptive Statistics by
——————————————————————————————————————————————————————————————
NSTEMI STEMI Unstable Angina p N=153) (N=304) (N=400)
(
—————————————————————————————————————————————————————————————— 4 ( 2.6%) 48 (15.8%) 0 ( 0.0%) 0.000
cardiogenicShock 57 (37.3%) 96 (31.6%) 151 (37.8%) 0.209
DM 47 (30.7%) 95 (31.2%) 148 (37.0%) 0.186
obesity 91 (59.5%) 154 (50.7%) 256 (64.0%) 0.002
HBP ——————————————————————————————————————————————————————————————
You can print mytable object in ‘html5’ or ‘LaTex’ format with ztable.
mytable(Dx~cardiogenicShock+DM+obesity+HBP,data=acs) %>% compress(add.label=FALSE) %>% ztable
p | ||||
---|---|---|---|---|
(N=153) | (N=304) | (N=400) | ||
cardiogenicShock | 4 ( 2.6%) | 48 (15.8%) | 0 ( 0.0%) | < 0.001 |
You can delete rows of an object of class mytable.
mytable(sex~Dx,data=acs)
'sex'
Descriptive Statistics by
——————————————————————————————————————————————————
Female Male p N=287) (N=570)
(
—————————————————————————————————————————————————— 0.012
Dx - NSTEMI 50 (17.4%) 103 (18.1%)
- STEMI 84 (29.3%) 220 (38.6%)
- Unstable Angina 153 (53.3%) 247 (43.3%)
——————————————————————————————————————————————————
If you want to delete the second row, use the deleteRows() function.
mytable(sex~Dx,data=acs) %>% deleteRows(2)
'sex'
Descriptive Statistics by
——————————————————————————————————————————————————
Female Male p N=287) (N=570)
(
—————————————————————————————————————————————————— 0.012
Dx - STEMI 84 (29.3%) 220 (38.6%)
- Unstable Angina 153 (53.3%) 247 (43.3%)
——————————————————————————————————————————————————
You can delete rows of an object of class cbind.mytable.
mytable(sex+HBP~age+Dx,data=acs) %>% deleteRows(3)
'sex' and 'HBP'
Descriptive Statistics stratified by
————————————————————————————————————————————————————————————————————————————————
Male Female
————————————————————————————— —————————————————————————————
No Yes p No Yes p N=273) (N=297) (N=83) (N=204)
(
———————————————————————————————————————————————————————————————————————————————— 57.5 ± 11.3 63.5 ± 10.4 0.000 68.0 ± 12.3 69.0 ± 10.0 0.526
age 0.016 0.084
Dx - STEMI 122 (44.7%) 98 (33.0%) 28 (33.7%) 56 (27.5%)
- Unstable Angina 108 (39.6%) 139 (46.8%) 36 (43.4%) 117 (57.4%)
————————————————————————————————————————————————————————————————————————————————
You can select method for categorical variables with catMethod
argument. Possible values are :
You can see which tests are used if you set show.all
argument of mytable TRUE.
mytable(obesity~HBP,data=acs,catMethod=1,show.all=TRUE)
'obesity'
Descriptive Statistics by
————————————————————————————————————————————————————————————————————————————————————————————————————
No Yes p sig p1 p2 p3 class ptest N N=567) (N=290)
(
———————————————————————————————————————————————————————————————————————————————————————————————————— 0.034 ** 0.034 2.000 categorical Pearson's Chi-squared test 857
HBP - No 250 (44.1%) 106 (36.6%)
- Yes 317 (55.9%) 184 (63.4%)
————————————————————————————————————————————————————————————————————————————————————————————————————
Sometimes, you want to display formatted numbers. For example, 1234.5
can be printed as 1,234.5
. You can do this using addComma() function
data(diamonds,package="ggplot2")
mytable(diamonds) %>% addComma
Descriptive Statistics
—————————————————————————————————————————————————————— % N Missing (%)
Mean ± SD or
—————————————————————————————————————————————————————— 0.8 ± 0.5 53,940 0 ( 0.0%)
carat 53,940 0 ( 0.0%)
cut - Fair 1610 (3.0%)
- Good 4906 (9.1%)
- Very Good 12082 (22.4%)
- Premium 13791 (25.6%)
- Ideal 21551 (40.0%)
53,940 0 ( 0.0%)
color - D 6775 (12.6%)
- E 9797 (18.2%)
- F 9542 (17.7%)
- G 11292 (20.9%)
- H 8304 (15.4%)
- I 5422 (10.1%)
- J 2808 (5.2%)
53,940 0 ( 0.0%)
clarity - I1 741 (1.4%)
- SI2 9194 (17.0%)
- SI1 13065 (24.2%)
- VS2 12258 (22.7%)
- VS1 8171 (15.1%)
- VVS2 5066 (9.4%)
- VVS1 3655 (6.8%)
- IF 1790 (3.3%)
61.7 ± 1.4 53,940 0 ( 0.0%)
depth 57.5 ± 2.2 53,940 0 ( 0.0%)
table 3932.8 ± 3989.4 53,940 0 ( 0.0%)
price 5.7 ± 1.1 53,940 0 ( 0.0%)
x 5.7 ± 1.1 53,940 0 ( 0.0%)
y 3.5 ± 0.7 53,940 0 ( 0.0%)
z ——————————————————————————————————————————————————————
Also you can print this in ‘html5’ or ‘LaTex’ format with ztable.
mytable(cut~.,data=diamonds) %>% addComma %>% ztable
p | ||||||
---|---|---|---|---|---|---|
(N= 1,610) | (N= 4,906) | (N=12,082) | (N=13,791) | (N=21,551) | ||
carat | 1.0 ± 0.5 | 0.8 ± 0.5 | 0.8 ± 0.5 | 0.9 ± 0.5 | 0.7 ± 0.4 | < 0.001 |