mattansb / Practical-Applications-in-R-for-Psychologists

Lesson files for Practical Applications in R for Psychologists.
Other
125 stars 17 forks source link

tableone #15

Open mattansb opened 3 years ago

mattansb commented 3 years ago
library(tableone)
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

CreateTableOne(data = iris, strata = "Species")
#>                           Stratified by Species
#>                            setosa        versicolor    virginica     p     
#>   n                          50            50            50                
#>   Sepal.Length (mean (SD)) 5.01 (0.35)   5.94 (0.52)   6.59 (0.64)   <0.001
#>   Sepal.Width (mean (SD))  3.43 (0.38)   2.77 (0.31)   2.97 (0.32)   <0.001
#>   Petal.Length (mean (SD)) 1.46 (0.17)   4.26 (0.47)   5.55 (0.55)   <0.001
#>   Petal.Width (mean (SD))  0.25 (0.11)   1.33 (0.20)   2.03 (0.27)   <0.001
#>   Species (%)                                                        <0.001
#>      setosa                  50 (100.0)     0 (  0.0)     0 (  0.0)        
#>      versicolor               0 (  0.0)    50 (100.0)     0 (  0.0)        
#>      virginica                0 (  0.0)     0 (  0.0)    50 (100.0)        
#>                           Stratified by Species
#>                            test
#>   n                            
#>   Sepal.Length (mean (SD))     
#>   Sepal.Width (mean (SD))      
#>   Petal.Length (mean (SD))     
#>   Petal.Width (mean (SD))      
#>   Species (%)                  
#>      setosa                    
#>      versicolor                
#>      virginica

mtcars %>% 
  mutate(across(c(cyl, am, vs, gear, carb), ordered)) %>% 
  CreateTableOne(data = ., strata = "cyl") %>% 
  print(nonnormal = c("hp", "drat"))
#>                      Stratified by cyl
#>                       4                     6                      
#>   n                       11                     7                 
#>   mpg (mean (SD))      26.66 (4.51)          19.74 (1.45)          
#>   cyl (%)                                                          
#>      4                    11 (100.0)             0 (  0.0)         
#>      6                     0 (  0.0)             7 (100.0)         
#>      8                     0 (  0.0)             0 (  0.0)         
#>   disp (mean (SD))    105.14 (26.87)        183.31 (41.56)         
#>   hp (median [IQR])    91.00 [65.50, 96.00] 110.00 [110.00, 123.00]
#>   drat (median [IQR])   4.08 [3.81, 4.16]     3.90 [3.35, 3.91]    
#>   wt (mean (SD))        2.29 (0.57)           3.12 (0.36)          
#>   qsec (mean (SD))     19.14 (1.68)          17.98 (1.71)          
#>   vs = 1 (%)              10 ( 90.9)             4 ( 57.1)         
#>   am = 1 (%)               8 ( 72.7)             3 ( 42.9)         
#>   gear (%)                                                         
#>      3                     1 (  9.1)             2 ( 28.6)         
#>      4                     8 ( 72.7)             4 ( 57.1)         
#>      5                     2 ( 18.2)             1 ( 14.3)         
#>   carb (%)                                                         
#>      1                     5 ( 45.5)             2 ( 28.6)         
#>      2                     6 ( 54.5)             0 (  0.0)         
#>      3                     0 (  0.0)             0 (  0.0)         
#>      4                     0 (  0.0)             4 ( 57.1)         
#>      6                     0 (  0.0)             1 ( 14.3)         
#>      8                     0 (  0.0)             0 (  0.0)         
#>                      Stratified by cyl
#>                       8                       p      test   
#>   n                       14                                
#>   mpg (mean (SD))      15.10 (2.56)           <0.001        
#>   cyl (%)                                     <0.001        
#>      4                     0 (  0.0)                        
#>      6                     0 (  0.0)                        
#>      8                    14 (100.0)                        
#>   disp (mean (SD))    353.10 (67.77)          <0.001        
#>   hp (median [IQR])   192.50 [176.25, 241.25] <0.001 nonnorm
#>   drat (median [IQR])   3.12 [3.07, 3.22]      0.001 nonnorm
#>   wt (mean (SD))        4.00 (0.76)           <0.001        
#>   qsec (mean (SD))     16.77 (1.20)            0.002        
#>   vs = 1 (%)               0 (  0.0)          <0.001        
#>   am = 1 (%)               2 ( 14.3)           0.013        
#>   gear (%)                                     0.001        
#>      3                    12 ( 85.7)                        
#>      4                     0 (  0.0)                        
#>      5                     2 ( 14.3)                        
#>   carb (%)                                     0.007        
#>      1                     0 (  0.0)                        
#>      2                     4 ( 28.6)                        
#>      3                     3 ( 21.4)                        
#>      4                     6 ( 42.9)                        
#>      6                     0 (  0.0)                        
#>      8                     1 (  7.1)

Created on 2021-02-22 by the reprex package (v1.0.0)

mattansb commented 2 years ago

Not needed.