kaz-yos / tableone

R package to create "Table 1", description of baseline characteristics with or without propensity score weighting
https://cran.r-project.org/web/packages/tableone/index.html
217 stars 41 forks source link

Display order of Binary variables #56

Open sino30535 opened 4 years ago

sino30535 commented 4 years ago

Hi,

Love this library and use a lot on my work. I have a question for printing tableone output. For binary categorical variable, is there an argument to choose which label to use? e.g. In surgeon specialty, I have Thoracic or Other value in the variable. Instead of printing SPECIALTY = Other (%), can I manually choose to display SPECIALTY = Thoracic (%)? I don't want to use showAllLevels mode because it would also display too many YES/NO variables that made my tableone unnecessarily long.

Thanks in advance

ndevln commented 4 years ago

Hi,

you can change the order of levels in categorical variables by using relevel() (?relevel)

A small example using a character variable:

character_var <- c("0 Thoracic", "1 Other")
releveld_var <- relevel(as.factor(character_var), ref ="1 Other")
# or
releveld_2_var <- factor(character_var, levels = c("1 Other",  "0 Thoracic"))

df <- data.frame(character_var, releveld_var, releveld_2_var)

tableone::CreateTableOne(factorVars = c("character_var", "releveld_var", "releveld_2_var"), data = df)
                                  Overall  
  n                               2        
  character_var = 1 Other (%)     1 (50.0) 
  releveld_var = 0 Thoracic (%)   1 (50.0) 
  releveld_2_var = 0 Thoracic (%) 1 (50.0) 
sino30535 commented 4 years ago

That's awesome, thank you so much.