moonBook 패키지 업데이트 안내
2018.05.26 15:29
moonBook 패키지가 0.2.0으로 업데이트 되었습니다. 주요 업데이트 내용은 (1) compress()함수와 (2) addComma 함수입니다.
(1) compress함수는 행에 있는 변수가 두군으로 나누어 지는 경우 세행으로 표시되던 것을 한 행으로 줄여 인쇄하는 기능입니다.
(2) addComma 함수는 숫자가 1000이상인 경우 comma를 붙여서 표시해주는 기능입니다.
다음 내용을 참조하세요.
Update in R package moonBook
Keon-Woong Moon
2018-05-26
Function “mytable”
Function “mytable”" produce table for descriptive analysis easily. It is most useful to make table to describe baseline charateristics common in medical research papers.
Basic Usage
If you are unfamiliar to package moonBook and mytable function, please see the R package moonBook
vignette at: https://cran.r-project.org/web/packages/moonBook/vignettes/moonBook.html
Explore a data.frame
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).
mytable(~age+sex,data=acs)
Descriptive Statistics
-------------------------------
N Total
-------------------------------
age 857 63.3 ± 11.7
sex 857
- Female 287 (33.5%)
- Male 570 (66.5%)
-------------------------------
Compress an object of class mytable
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.
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 |
For formatted numbers: addComma()
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.0 (9.1%)
- Very Good 12,082.0 (22.4%)
- Premium 13,791.0 (25.6%)
- Ideal 21,551 (40%)
color 53,940
- D 6,775.0 (12.6%)
- E 9,797.0 (18.2%)
- F 9,542.0 (17.7%)
- G 11,292.0 (20.9%)
- H 8,304.0 (15.4%)
- I 5,422.0 (10.1%)
- J 2,808.0 (5.2%)
clarity 53,940
- I1 741.0 (1.4%)
- SI2 9,194 (17%)
- SI1 13,065.0 (24.2%)
- VS2 12,258.0 (22.7%)
- VS1 8,171.0 (15.1%)
- VVS2 5,066.0 (9.4%)
- VVS1 3,655.0 (6.8%)
- IF 1,790.0 (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.
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 |