메뉴 건너뛰기

웹에서 하는 R 통계

moonBook 패키지 업데이트 안내

2018.05.26 15:29

cardiomoon 조회 수:307

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
NSTEMISTEMIUnstable Anginap
(N=153)(N=304)(N=400)
cardiogenicShock4 ( 2.6%)48 (15.8%)0 ( 0.0%)< 0.001
DM57 (37.3%)96 (31.6%)151 (37.8%)0.209
obesity47 (30.7%)95 (31.2%)148 (37.0%)0.186
HBP91 (59.5%)154 (50.7%)256 (64.0%)0.002

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
FairGoodVery GoodPremiumIdealp
(N= 1,610)(N= 4,906)(N=12,082)(N=13,791)(N=21,551)
carat1.0 ± 0.50.8 ± 0.50.8 ± 0.50.9 ± 0.50.7 ± 0.4< 0.001
color< 0.001
    D163.0 (10.1%)662.0 (13.5%)1,513.0 (12.5%)1,603.0 (11.6%)2,834.0 (13.2%)
    E224.0 (13.9%)933 (19%)2,400.0 (19.9%)2,337.0 (16.9%)3,903.0 (18.1%)
    F312.0 (19.4%)909.0 (18.5%)2,164.0 (17.9%)2,331.0 (16.9%)3,826.0 (17.8%)
    G314.0 (19.5%)871.0 (17.8%)2,299 (19%)2,924.0 (21.2%)4,884.0 (22.7%)
    H303.0 (18.8%)702.0 (14.3%)1,824.0 (15.1%)2,360.0 (17.1%)3,115.0 (14.5%)
    I175.0 (10.9%)522.0 (10.6%)1,204 (10%)1,428.0 (10.4%)2,093.0 (9.7%)
    J119.0 (7.4%)307.0 (6.3%)678.0 (5.6%)808.0 (5.9%)896.0 (4.2%)
clarity< 0.001
    I1210 (13%)96 (2%)84.0 (0.7%)205.0 (1.5%)146.0 (0.7%)
    SI2466.0 (28.9%)1,081 (22%)2,100.0 (17.4%)2,949.0 (21.4%)2,598.0 (12.1%)
    SI1408.0 (25.3%)1,560.0 (31.8%)3,240.0 (26.8%)3,575.0 (25.9%)4,282.0 (19.9%)
    VS2261.0 (16.2%)978.0 (19.9%)2,591.0 (21.4%)3,357.0 (24.3%)5,071.0 (23.5%)
    VS1170.0 (10.6%)648.0 (13.2%)1,775.0 (14.7%)1,989.0 (14.4%)3,589.0 (16.7%)
    VVS269.0 (4.3%)286.0 (5.8%)1,235.0 (10.2%)870.0 (6.3%)2,606.0 (12.1%)
    VVS117.0 (1.1%)186.0 (3.8%)789.0 (6.5%)616.0 (4.5%)2,047.0 (9.5%)
    IF9.0 (0.6%)71.0 (1.4%)268.0 (2.2%)230.0 (1.7%)1,212.0 (5.6%)
depth64.0 ± 3.662.4 ± 2.261.8 ± 1.461.3 ± 1.261.7 ± 0.7< 0.001
table59.1 ± 3.958.7 ± 2.958.0 ± 2.158.7 ± 1.556.0 ± 1.2< 0.001
price4,358.8 ± 3,560.43,928.9 ± 3,681.63,981.8 ± 3,935.94,584.3 ± 4,349.23,457.5 ± 3,808.4< 0.001
x6.2 ± 1.05.8 ± 1.15.7 ± 1.16.0 ± 1.25.5 ± 1.1< 0.001
z4.0 ± 0.73.6 ± 0.73.6 ± 0.73.6 ± 0.73.4 ± 0.7< 0.001

번호 제목 글쓴이 날짜 조회 수
공지 웹에서 하는 R 메타분석 [1] cardiomoon 2015.11.11 4756
공지 웹에서 하는 R 통계 게시판입니다. cardiomoon 2015.03.26 1252
80 Kormaps 패키지를 이용한 단계구분도 쉽게 그리기(1) [8] cardiomoon 2016.03.08 10234
79 웹에서 하는 R 메타분석 설명서 cardiomoon 2015.12.07 5089
78 다중회귀모형에서 상호작용의 시각화 [2] cardiomoon 2016.02.16 5049
77 ggplot2 multiplot with error bars cardiomoon 2015.06.29 4860
76 moonBook 패키지 업데이트 안내 [3] cardiomoon 2018.05.18 1755
75 웹R에서 boxplot 그리기 cardiomoon 2015.07.11 1723
74 다중회귀모형에서 설명변수들의 상대적 중요성 [1] cardiomoon 2016.02.15 1607
73 샤이니 앱 : interactive ggplot [1] file cardiomoon 2017.02.28 1569
72 한국행정지도(2014) 패키지 kormaps2014 안내 [3] cardiomoon 2016.10.31 1559
71 웹에서 하는 R통계 v2.0 베타버전을 공개합니다. [1] cardiomoon 2016.03.14 1457
70 ggplot2에서 만든 파워포인트파일 [1] file cardiomoon 2016.01.01 1345
69 R에서 데이터 편집을 하자 - editData 패키지 [1] cardiomoon 2017.09.25 1320
68 웹에서 하는 R구조방정식(1) Getting started cardiomoon 2015.12.27 1282
67 생존분석에서 연속형 영향변수의 cutpoint 구하기 [2] cardiomoon 2015.03.31 1203
66 "틀리지않는법" 강의슬라이드(문건웅) cardiomoon 2017.08.31 1200
65 메타분석 강의록입니다. [2] file cardiomoon 2015.11.24 1117
64 교차 검증 (cross-validation) 관련 [9] 장진희 2015.09.01 1107
63 웹에서 하는 R구조방정식 앱 정식 공개 [2] cardiomoon 2016.03.02 1106