Function “mytable”" produce table for descriptive analysis easily. It is most useful to make table to describe baseline charateristics 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 Total
----------------------------------------
age 857 63.3 ± 11.7
sex 857
- Female 287 (33.5%)
- Male 570 (66.5%)
cardiogenicShock 857
- No 805 (93.9%)
- Yes 52 (6.1%)
entry 857
- Femoral 312 (36.4%)
- Radial 545 (63.6%)
Dx 857
- NSTEMI 153 (17.9%)
- STEMI 304 (35.5%)
- Unstable Angina 400 (46.7%)
EF 723 55.8 ± 9.6
height 764 163.2 ± 9.1
weight 766 64.8 ± 11.4
BMI 764 24.3 ± 3.3
obesity 857
- No 567 (66.2%)
- Yes 290 (33.8%)
TC 834 185.2 ± 47.8
LDLC 833 116.6 ± 41.1
HDLC 834 38.2 ± 11.1
TG 842 125.2 ± 90.9
DM 857
- No 553 (64.5%)
- Yes 304 (35.5%)
HBP 857
- No 356 (41.5%)
- Yes 501 (58.5%)
smoking 857
- Ex-smoker 204 (23.8%)
- Never 332 (38.7%)
- Smoker 321 (37.5%)
----------------------------------------
You can use formula without grouping variable(s).
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%)
---------------------------------------------------------
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
-------------------------------------------------------
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
-------------------------------------------------------
Sometimes it is more simple to omit the represntative 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
------------------------------------------------------------------
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
--------------------------------------------------------------
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 obect 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%)
--------------------------------------------------
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%)
--------------------------------------------------
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%)
--------------------------------------------------------------------------------
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%)
----------------------------------------------------------------------------------------------------
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 Total
------------------------------------------
carat 53,940 0.8 ± 0.5
cut 53,940
- Fair 1,610 (3%)
- Good 4,906 (9.1%)
- Very Good 12,082 (22.4%)
- Premium 13,791 (25.6%)
- Ideal 21,551 (40%)
color 53,940
- D 6,775 (12.6%)
- E 9,797 (18.2%)
- F 9,542 (17.7%)
- G 11,292 (20.9%)
- H 8,304 (15.4%)
- I 5,422 (10.1%)
- J 2,808 (5.2%)
clarity 53,940
- I1 741 (1.4%)
- SI2 9,194 (17%)
- SI1 13,065 (24.2%)
- VS2 12,258 (22.7%)
- VS1 8,171 (15.1%)
- VVS2 5,066 (9.4%)
- VVS1 3,655 (6.8%)
- IF 1,790 (3.3%)
depth 53,940 61.7 ± 1.4
table 53,940 57.5 ± 2.2
price 53,940 3,932.8 ± 3,989.4
x 53,940 5.7 ± 1.1
y 53,940 5.7 ± 1.1
z 53,940 3.5 ± 0.7
------------------------------------------
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 |