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
————————————————————————————————————————————————————————
Mean ± SD or % N Missing (%)
————————————————————————————————————————————————————————
age 63.3 ± 11.7 857 0 ( 0.0%)
sex 857 0 ( 0.0%)
- Female 287 (33.5%)
- Male 570 (66.5%)
cardiogenicShock 857 0 ( 0.0%)
- No 805 (93.9%)
- Yes 52 (6.1%)
entry 857 0 ( 0.0%)
- Femoral 312 (36.4%)
- Radial 545 (63.6%)
Dx 857 0 ( 0.0%)
- NSTEMI 153 (17.9%)
- STEMI 304 (35.5%)
- Unstable Angina 400 (46.7%)
EF 55.8 ± 9.6 723 134 (15.6%)
height 163.2 ± 9.1 764 93 (10.9%)
weight 64.8 ± 11.4 766 91 (10.6%)
BMI 24.3 ± 3.3 764 93 (10.9%)
obesity 857 0 ( 0.0%)
- No 567 (66.2%)
- Yes 290 (33.8%)
TC 185.2 ± 47.8 834 23 ( 2.7%)
LDLC 116.6 ± 41.1 833 24 ( 2.8%)
HDLC 38.2 ± 11.1 834 23 ( 2.7%)
TG 125.2 ± 90.9 842 15 ( 1.8%)
DM 857 0 ( 0.0%)
- No 553 (64.5%)
- Yes 304 (35.5%)
HBP 857 0 ( 0.0%)
- No 356 (41.5%)
- Yes 501 (58.5%)
smoking 857 0 ( 0.0%)
- 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
—————————————————————————————————————————————
Mean ± SD or % N Missing (%)
—————————————————————————————————————————————
age 63.3 ± 11.7 857 0 ( 0.0%)
sex 857 0 ( 0.0%)
- 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)
Descriptive Statistics by 'Dx'
—————————————————————————————————————————————————————————
NSTEMI STEMI Unstable Angina p
(N=153) (N=304) (N=400)
—————————————————————————————————————————————————————————
sex 0.012
- Female 50 (32.7%) 84 (27.6%) 153 (38.2%)
- Male 103 (67.3%) 220 (72.4%) 247 (61.8%)
—————————————————————————————————————————————————————————
Data summarized as: Mean ± SD or %
mytable(Dx~sex,data=acs) %>% compress
Descriptive Statistics by 'Dx'
———————————————————————————————————————————————————————
NSTEMI STEMI Unstable Angina p
(N=153) (N=304) (N=400)
———————————————————————————————————————————————————————
sex:Male 103 (67.3%) 220 (72.4%) 247 (61.8%) 0.012
———————————————————————————————————————————————————————
Data summarized as: Mean ± SD or %
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)
Descriptive Statistics by 'Dx'
———————————————————————————————————————————————————————
NSTEMI STEMI Unstable Angina p
(N=153) (N=304) (N=400)
———————————————————————————————————————————————————————
sex:Female 50 (32.7%) 84 (27.6%) 153 (38.2%) 0.012
———————————————————————————————————————————————————————
Data summarized as: Mean ± SD or %
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
Descriptive Statistics by 'Dx'
——————————————————————————————————————————————————————————————————
NSTEMI STEMI Unstable Angina p
(N=153) (N=304) (N=400)
——————————————————————————————————————————————————————————————————
cardiogenicShock:Yes 4 ( 2.6%) 48 (15.8%) 0 ( 0.0%) 0.000
DM:Yes 57 (37.3%) 96 (31.6%) 151 (37.8%) 0.209
obesity:Yes 47 (30.7%) 95 (31.2%) 148 (37.0%) 0.186
HBP:Yes 91 (59.5%) 154 (50.7%) 256 (64.0%) 0.002
——————————————————————————————————————————————————————————————————
Data summarized as: Mean ± SD or %
mytable(Dx~cardiogenicShock+DM+obesity+HBP,data=acs) %>% compress(add.label=FALSE)
Descriptive Statistics by 'Dx'
——————————————————————————————————————————————————————————————
NSTEMI STEMI Unstable Angina p
(N=153) (N=304) (N=400)
——————————————————————————————————————————————————————————————
cardiogenicShock 4 ( 2.6%) 48 (15.8%) 0 ( 0.0%) 0.000
DM 57 (37.3%) 96 (31.6%) 151 (37.8%) 0.209
obesity 47 (30.7%) 95 (31.2%) 148 (37.0%) 0.186
HBP 91 (59.5%) 154 (50.7%) 256 (64.0%) 0.002
——————————————————————————————————————————————————————————————
Data summarized as: Mean ± SD or %
You can print mytable object in ‘html5’ or ‘LaTex’ format with 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)
Descriptive Statistics by 'sex'
——————————————————————————————————————————————————
Female Male p
(N=287) (N=570)
——————————————————————————————————————————————————
Dx 0.012
- NSTEMI 50 (17.4%) 103 (18.1%)
- STEMI 84 (29.3%) 220 (38.6%)
- Unstable Angina 153 (53.3%) 247 (43.3%)
——————————————————————————————————————————————————
Data summarized as: Mean ± SD or %
If you want to delete the second row, use the deleteRows() function.
mytable(sex~Dx,data=acs) %>% deleteRows(2)
Descriptive Statistics by 'sex'
——————————————————————————————————————————————————
Female Male p
(N=287) (N=570)
——————————————————————————————————————————————————
Dx 0.012
- STEMI 84 (29.3%) 220 (38.6%)
- Unstable Angina 153 (53.3%) 247 (43.3%)
——————————————————————————————————————————————————
Data summarized as: Mean ± SD or %
You can delete rows of an object of class cbind.mytable.
mytable(sex+HBP~age+Dx,data=acs) %>% deleteRows(3)
Descriptive Statistics stratified by 'sex' and 'HBP'
————————————————————————————————————————————————————————————————————————————————
Male Female
————————————————————————————— —————————————————————————————
No Yes p No Yes p
(N=273) (N=297) (N=83) (N=204)
————————————————————————————————————————————————————————————————————————————————
age 57.5 ± 11.3 63.5 ± 10.4 0.000 68.0 ± 12.3 69.0 ± 10.0 0.526
Dx 0.016 0.084
- 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%)
————————————————————————————————————————————————————————————————————————————————
Data summarized as: Mean ± SD or %
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)
Descriptive Statistics by 'obesity'
————————————————————————————————————————————————————————————————————————————————————————————————————
No Yes p sig p1 p2 p3 class ptest N
(N=567) (N=290)
————————————————————————————————————————————————————————————————————————————————————————————————————
HBP 0.034 ** 0.034 2.000 categorical Pearson's Chi-squared test 857
- No 250 (44.1%) 106 (36.6%)
- Yes 317 (55.9%) 184 (63.4%)
————————————————————————————————————————————————————————————————————————————————————————————————————
Data summarized as: Mean ± SD or %
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
——————————————————————————————————————————————————————
Mean ± SD or % N Missing (%)
——————————————————————————————————————————————————————
carat 0.8 ± 0.5 53,940 0 ( 0.0%)
cut 53,940 0 ( 0.0%)
- Fair 1610 (3.0%)
- Good 4906 (9.1%)
- Very Good 12082 (22.4%)
- Premium 13791 (25.6%)
- Ideal 21551 (40.0%)
color 53,940 0 ( 0.0%)
- 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%)
clarity 53,940 0 ( 0.0%)
- 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%)
depth 61.7 ± 1.4 53,940 0 ( 0.0%)
table 57.5 ± 2.2 53,940 0 ( 0.0%)
price 3932.8 ± 3989.4 53,940 0 ( 0.0%)
x 5.7 ± 1.1 53,940 0 ( 0.0%)
y 5.7 ± 1.1 53,940 0 ( 0.0%)
z 3.5 ± 0.7 53,940 0 ( 0.0%)
——————————————————————————————————————————————————————
Also you can print this in ‘html5’ or ‘LaTex’ format with 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 |