moonBook 패키지 업데이트 안내
2018.05.18 12:18
moonBook 패키지가 업데이트 되었습니다.
1) mytable함수를 데이타프레임에 사용하거나 formula를 사용하되 group을 지정하지 않고 사용할 수 있습니다.
2) mytable함수에서 label을 사용할 수 있습니다.
CRAN 패키지 버젼은 0.1.8이니 최신버젼을 사용하시려면 github에서 하셔요.
if(!require(devtools)) install.packages("devtools")
devtools::install_github("cardiomoon/moonBook")
아래는 간략한 update내용입니다. 자세한 사항은 moonBook vignette(http://rpubs.com/cardiomoon/390168) 와 ztable update vignette(http://rpubs.com/cardiomoon/388645)를 참조하세요
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
require(moonBook)
Loading required package: moonBook
data(acs)
mytable(Dx~.,data=acs)
Descriptive Statistics by 'Dx'
_________________________________________________________________
NSTEMI STEMI Unstable Angina p
(N=153) (N=304) (N=400)
-----------------------------------------------------------------
age 64.3 ± 12.3 62.1 ± 12.1 63.8 ± 11.0 0.073
sex 0.012
- Female 50 (32.7%) 84 (27.6%) 153 (38.2%)
- Male 103 (67.3%) 220 (72.4%) 247 (61.8%)
cardiogenicShock 0.000
- No 149 (97.4%) 256 (84.2%) 400 (100.0%)
- Yes 4 ( 2.6%) 48 (15.8%) 0 ( 0.0%)
entry 0.001
- Femoral 58 (37.9%) 133 (43.8%) 121 (30.2%)
- Radial 95 (62.1%) 171 (56.2%) 279 (69.8%)
EF 55.0 ± 9.3 52.4 ± 9.5 59.2 ± 8.7 0.000
height 163.3 ± 8.2 165.1 ± 8.2 161.7 ± 9.7 0.000
weight 64.3 ± 10.2 65.7 ± 11.6 64.5 ± 11.6 0.361
BMI 24.1 ± 3.2 24.0 ± 3.3 24.6 ± 3.4 0.064
obesity 0.186
- No 106 (69.3%) 209 (68.8%) 252 (63.0%)
- Yes 47 (30.7%) 95 (31.2%) 148 (37.0%)
TC 193.7 ± 53.6 183.2 ± 43.4 183.5 ± 48.3 0.057
LDLC 126.1 ± 44.7 116.7 ± 39.5 112.9 ± 40.4 0.004
HDLC 38.9 ± 11.9 38.5 ± 11.0 37.8 ± 10.9 0.501
TG 130.1 ± 88.5 106.5 ± 72.0 137.4 ± 101.6 0.000
DM 0.209
- No 96 (62.7%) 208 (68.4%) 249 (62.2%)
- Yes 57 (37.3%) 96 (31.6%) 151 (37.8%)
HBP 0.002
- No 62 (40.5%) 150 (49.3%) 144 (36.0%)
- Yes 91 (59.5%) 154 (50.7%) 256 (64.0%)
smoking 0.000
- Ex-smoker 42 (27.5%) 66 (21.7%) 96 (24.0%)
- Never 50 (32.7%) 97 (31.9%) 185 (46.2%)
- Smoker 61 (39.9%) 141 (46.4%) 119 (29.8%)
-----------------------------------------------------------------
The first argument of function mytable
is an object of class formula
. Left side of ~ must contain the name of one grouping variable or two grouping variables in an additive way(e.g. sex+group~), and the right side of ~ must have variables in an additive way. .
is allowed on the right side of formula which means all variables in the data.frame specified by the 2nd argument data
. The sample data ‘acs’ containing demographic data and laboratory data of 857 pateints with acute coronary syndrome(ACS). For more information about the data acs, type ?acs in your R console.
str(acs)
'data.frame': 857 obs. of 17 variables:
$ age : int 62 78 76 89 56 73 58 62 59 71 ...
$ sex : chr "Male" "Female" "Female" "Female" ...
$ cardiogenicShock: chr "No" "No" "Yes" "No" ...
$ entry : chr "Femoral" "Femoral" "Femoral" "Femoral" ...
$ Dx : chr "STEMI" "STEMI" "STEMI" "STEMI" ...
$ EF : num 18 18.4 20 21.8 21.8 22 24.7 26.6 28.5 31.1 ...
$ height : num 168 148 NA 165 162 153 167 160 152 168 ...
$ weight : num 72 48 NA 50 64 59 78 50 67 60 ...
$ BMI : num 25.5 21.9 NA 18.4 24.4 ...
$ obesity : chr "Yes" "No" "No" "No" ...
$ TC : num 215 NA NA 121 195 184 161 136 239 169 ...
$ LDLC : int 154 NA NA 73 151 112 91 88 161 88 ...
$ HDLC : int 35 NA NA 20 36 38 34 33 34 54 ...
$ TG : int 155 166 NA 89 63 137 196 30 118 141 ...
$ DM : chr "Yes" "No" "No" "No" ...
$ HBP : chr "No" "Yes" "Yes" "No" ...
$ smoking : chr "Smoker" "Never" "Never" "Never" ...
Explore a data.frame
You can use mytable
function to explore a data.frame.
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%)
-----------------------------
다음은 라벨의 사용에 관한 내용입니다.
Use of labelled data
You can use labels of the data. For example, you can add column labels and value labels of mtcars
data using set_label() and set_labels() function of the sjlabelled
package.
if(!require(sjlabelled)) {
install.packages("sjlabelled")
require(sjlabelled)
}
Loading required package: sjlabelled
df=mtcars
df$am<-set_label(df$am,label="Transmission")
df$am<-set_labels(df$am,labels=c("automatic","manual"))
df$vs<-set_label(df$vs,label="Engine")
df$vs<-set_labels(df$vs,labels=c("V-shaped","straight"))
You can see the labels of this data.
str(df)
'data.frame': 32 obs. of 11 variables:
$ mpg : num 21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
$ cyl : num 6 6 4 6 8 6 8 4 4 6 ...
$ disp: num 160 160 108 258 360 ...
$ hp : num 110 110 93 110 175 105 245 62 95 123 ...
$ drat: num 3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
$ wt : num 2.62 2.88 2.32 3.21 3.44 ...
$ qsec: num 16.5 17 18.6 19.4 17 ...
$ vs : num 0 0 1 1 0 1 0 1 1 1 ...
..- attr(*, "label")= chr "Engine"
..- attr(*, "labels")= Named num 0 1
.. ..- attr(*, "names")= chr "V-shaped" "straight"
$ am : num 1 1 1 0 0 0 0 0 0 0 ...
..- attr(*, "label")= chr "Transmission"
..- attr(*, "labels")= Named num 0 1
.. ..- attr(*, "names")= chr "automatic" "manual"
$ gear: num 4 4 4 3 3 3 3 4 4 4 ...
$ carb: num 4 4 1 1 2 1 4 2 2 4 ...
You can use the column label and value labels with mytable() function. The default values of use.labels
and use.column.labels
arguments of mytable() function are TRUE
. You can change this arguments if you want.
mytable(df)
Descriptive Statistics
--------------------------------
N Total
--------------------------------
mpg 32 20.1 ± 6.0
cyl 32
- 4 11 (34.4%)
- 6 7 (21.9%)
- 8 14 (43.8%)
disp 32 230.7 ± 123.9
hp 32 146.7 ± 68.6
drat 32 3.6 ± 0.5
wt 32 3.2 ± 1.0
qsec 32 17.8 ± 1.8
Engine 32
- V-shaped 18 (56.2%)
- straight 14 (43.8%)
Transmission 32
- automatic 19 (59.4%)
- manual 13 (40.6%)
gear 32
- 3 15 (46.9%)
- 4 12 (37.5%)
- 5 5 (15.6%)
carb 32 2.8 ± 1.6
--------------------------------
mytable(df,use.labels=FALSE, use.column.label=FALSE)
Descriptive Statistics
------------------------
N Total
------------------------
mpg 32 20.1 ± 6.0
cyl 32
- 4 11 (34.4%)
- 6 7 (21.9%)
- 8 14 (43.8%)
disp 32 230.7 ± 123.9
hp 32 146.7 ± 68.6
drat 32 3.6 ± 0.5
wt 32 3.2 ± 1.0
qsec 32 17.8 ± 1.8
vs 32
- 0 18 (56.2%)
- 1 14 (43.8%)
am 32
- 0 19 (59.4%)
- 1 13 (40.6%)
gear 32
- 3 15 (46.9%)
- 4 12 (37.5%)
- 5 5 (15.6%)
carb 32 2.8 ± 1.6
------------------------
mytable(am~.,data=df)
Descriptive Statistics by 'Transmission'
______________________________________________
automatic manual p
(N=19) (N=13)
----------------------------------------------
mpg 17.1 ± 3.8 24.4 ± 6.2 0.000
cyl 0.013
- 4 3 (15.8%) 8 (61.5%)
- 6 4 (21.1%) 3 (23.1%)
- 8 12 (63.2%) 2 (15.4%)
disp 290.4 ± 110.2 143.5 ± 87.2 0.000
hp 160.3 ± 53.9 126.8 ± 84.1 0.180
drat 3.3 ± 0.4 4.0 ± 0.4 0.000
wt 3.8 ± 0.8 2.4 ± 0.6 0.000
qsec 18.2 ± 1.8 17.4 ± 1.8 0.206
Engine 0.556
- V-shaped 12 (63.2%) 6 (46.2%)
- straight 7 (36.8%) 7 (53.8%)
gear 0.000
- 3 15 (78.9%) 0 ( 0.0%)
- 4 4 (21.1%) 8 (61.5%)
- 5 0 ( 0.0%) 5 (38.5%)
carb 2.7 ± 1.1 2.9 ± 2.2 0.781
----------------------------------------------
mytable(vs+am~.,data=df)
Descriptive Statistics stratified by 'Engine' and 'Transmission'
_____________________________________________________________________
V-shaped straight
------------------------------- ------------------------------
automatic manual p automatic manual p
(N=12) (N=6) (N=7) (N=7)
---------------------------------------------------------------------
mpg 15.1 ± 2.8 19.8 ± 4.0 0.010 20.7 ± 2.5 28.4 ± 4.8 0.003
cyl 0.006
- 4 0 ( 0.0%) 1 (16.7%) 3 (42.9%) 7 (100.0%)
- 6 0 ( 0.0%) 3 (50.0%) 4 (57.1%) 0 ( 0.0%)
- 8 12 (100.0%) 2 (33.3%) 0 ( 0.0%) 0 ( 0.0%)
disp 357.6 ± 71.8 206.2 ± 95.2 0.002 175.1 ± 49.1 89.8 ± 18.8 0.003
hp 194.2 ± 33.4 180.8 ± 98.8 0.760 102.1 ± 20.9 80.6 ± 24.1 0.099
drat 3.1 ± 0.2 3.9 ± 0.3 0.000 3.6 ± 0.5 4.1 ± 0.4 0.025
wt 4.1 ± 0.8 2.9 ± 0.5 0.002 3.2 ± 0.3 2.0 ± 0.4 0.000
qsec 17.1 ± 0.8 15.8 ± 1.1 0.009 20.0 ± 1.5 18.7 ± 0.9 0.078
gear 0.000 0.111
- 3 12 (100.0%) 0 ( 0.0%) 3 (42.9%) 0 ( 0.0%)
- 4 0 ( 0.0%) 2 (33.3%) 4 (57.1%) 6 (85.7%)
- 5 0 ( 0.0%) 4 (66.7%) 0 ( 0.0%) 1 (14.3%)
carb
- 1 0 ( 0.0%) 0 ( 0.0%) 3 (42.9%) 4 (57.1%)
- 2 4 (33.3%) 1 (16.7%) 2 (28.6%) 3 (42.9%)
- 3 3 (25.0%) 0 ( 0.0%) 0 ( 0.0%) 0 ( 0.0%)
- 4 5 (41.7%) 3 (50.0%) 2 (28.6%) 0 ( 0.0%)
- 6 0 ( 0.0%) 1 (16.7%) 0 ( 0.0%) 0 ( 0.0%)
- 8 0 ( 0.0%) 1 (16.7%) 0 ( 0.0%) 0 ( 0.0%)
---------------------------------------------------------------------
안녕하세요 선생님
패키지 이용시 기본값이 소수점 한자리까지 나오는데 소수점 두자리까지 나오게 하게 할 수는 없을까요?