Search code examples
rrowpercentage

How to use tableone to change table percentage by row?


I am use library(tableone) to make my descriptive statistics for multiple variables This is my code:

library(tableone)

myVars <- c("class", "age", "Sex", "bmi", "bmi_category",
            "drink_freq", "smoke_yn", "edu_dummy")

catVars <- c("class", "Sex", "bmi_category",
             "drink_freq", "smoke_yn", "edu_dummy")

tab1_inf <- CreateTableOne(vars = myVars, strata = "NEWDI",
                             data = TKA_table1, factorVars = catVars)

a1 <- print(tab1_inf, exact = "NEWDI", showAllLevels = TRUE)

This it default for percentage, and I want change it format like this(example): enter image description here

I checked its description and found no options to set. https://rdrr.io/cran/tableone/man/print.TableOne.html How can I do it?


Solution

  • With some clever getting-your-hands dirty, you can manipulate the percentages in the TableOne object. This uses an example dataset called pbc from survival package.

    library(tableone)
    library(survival)
    data(pbc)
    
    ## Make categorical variables factors
    varsToFactor <- c("status","trt","ascites","hepato","spiders","edema","stage")
    pbc[varsToFactor] <- lapply(pbc[varsToFactor], factor)
    
    ## Create a variable list
    vars <- c("time","status","age","sex","ascites","hepato",
              "spiders","edema","bili","chol","albumin",
              "copper","alk.phos","ast","trig","platelet",
              "protime","stage")
    
    ## Create Table 1 stratified by trt
    tableOne <- CreateTableOne(vars = vars, strata = c("trt"), data = pbc)
    
    tableOne
    

    Before

                          Stratified by trt
                           1                 2                 p      test
      n                        158               154                      
      time (mean (SD))     2015.62 (1094.12) 1996.86 (1155.93)  0.883     
      status (%)                                                0.894     
         0                      83 (52.5)         85 (55.2)               
         1                      10 ( 6.3)          9 ( 5.8)               
         2                      65 (41.1)         60 (39.0)               
      age (mean (SD))        51.42 (11.01)     48.58 (9.96)     0.018     
      sex = f (%)              137 (86.7)        139 (90.3)     0.421     
      ascites = 1 (%)           14 ( 8.9)         10 ( 6.5)     0.567     
      hepato = 1 (%)            73 (46.2)         87 (56.5)     0.088     
      spiders = 1 (%)           45 (28.5)         45 (29.2)     0.985 
    ...
    

    You should try to adapt the following code for your own data format:

    for (i in 1:length(table1)) {
        sum = tableOne$CatTable[[1]][[i]]$freq + tableOne$CatTable[[2]][[i]]$freq
        tableOne$CatTable[[1]][[i]]$percent = tableOne$CatTable[[1]][[i]]$freq / sum
        tableOne$CatTable[[2]][[i]]$percent = tableOne$CatTable[[2]][[i]]$freq / sum
      }
    }
    
    tableOne
    

    After

                          Stratified by trt
                           1                 2                 p      test
      n                        158               154                      
      time (mean (SD))     2015.62 (1094.12) 1996.86 (1155.93)  0.883     
      status (%)                                                0.894     
         0                      83 (0.5)          85 (0.5)                
         1                      10 (0.5)           9 (0.5)                
         2                      65 (0.5)          60 (0.5)                
      age (mean (SD))        51.42 (11.01)     48.58 (9.96)     0.018     
      sex = f (%)              137 (0.5)         139 (0.5)      0.421     
      ascites = 1 (%)           14 (0.6)          10 (0.4)      0.567     
      hepato = 1 (%)            73 (0.5)          87 (0.5)      0.088     
      spiders = 1 (%)           45 (0.5)          45 (0.5)      0.985