rlabeloutputcrosstabsummarytools

How do I rename row & column variable name output using ctable from R's summarytools package?


I'm a big fan of R's simple functions like table() for quickly viewing bivariate tables but when I want frequencies & percentages in the same output, I use ctable() from the summarytools package.

Sometimes I have long variable names, which is no problem for table(); I just do a quick rename in the table() function. However, I haven't figured out how to do the same sort of variable label renaming for the ctable() output. I found a few posts on SO about ctable() and labeling, but these pertained to other, more complex work such as involving kable() and Rmarkdown. I'm looking to keep everything tucked within the raw console output, preferable all within ctable() or perhaps nested inside another function like print().

Here are code examples of what I'd like to do and where I'm stuck so far.

# load packages
library(summarytools)
# load example data - actual values don't matter here
data("mtcars")
# copy a variable so it has a long name
mtcars$really_long_variable_name_for_vs <- mtcars$vs

Here's a simple table() output example.

Ugly with no labels: table(mtcars$gear, mtcars$really_long_variable_name_for_vs)

   0  1
3 12  3
4  2 10
5  4  1

Easy to add row & column names: table("gear"=mtcars$gear, "vs"=mtcars$really_long_variable_name_for_vs))

    vs  
gear  0  1
   3 12  3
   4  2 10
   5  4  1

But not so straightforward with ctable() ... that I know of

Ugly long column name pushing the output to the right: ctable(mtcars$gear, mtcars$really_long_variable_name_for_vs)

Cross-Tabulation, Row Proportions  
gear * really_long_variable_name_for_vs  
Data Frame: mtcars  

------- ---------------------------------- ------------ ------------ -------------
          really_long_variable_name_for_vs            0            1         Total
   gear                                                                           
      3                                      12 (80.0%)    3 (20.0%)   15 (100.0%)
      4                                       2 (16.7%)   10 (83.3%)   12 (100.0%)
      5                                       4 (80.0%)    1 (20.0%)    5 (100.0%)
  Total                                      18 (56.2%)   14 (43.8%)   32 (100.0%)
------- ---------------------------------- ------------ ------------ -------------

Trying to rename like table() doesn't work. ctable("gear"=mtcars$gear, "vs"=mtcars$really_long_variable_name_for_vs) which produces the following error.

Error in ctable(gear = mtcars$gear, vs = mtcars$really_long_variable_name_for_vs) : argument "x" is missing, with no default

I understand the "x" missing given ctable() argument requirements, but not how to rename the x and y names in the output. The summarytools package vignettes (here) show re-labeling a variable with freq() and print(), but it didn't work so well when I tried a few versions for ctable() like this one: print(ctable(x=mtcars$gear, y=mtcars$really_long_variable_name_for_vs), Variable.label=c("RowVar","ColVar")) which gives the same output as above.

What I'd like to have output is something like this:

    Cross-Tabulation, Row Proportions  
gear * mtcars$really_long_variable_name_for_vs  
Data Frame: mtcars  

------- -- ----------- -----------  ------------
        vs          0            1         Total
   gear                                                                           
      3     12 (80.0%)    3 (20.0%)   15 (100.0%)
      4      2 (16.7%)   10 (83.3%)   12 (100.0%)
      5      4 (80.0%)    1 (20.0%)    5 (100.0%)
  Total     18 (56.2%)   14 (43.8%)   32 (100.0%)
------- -- ----------- ------------ -------------

What am I missing here? Thanks in advance!


Solution

  • Use dnn :

    library(summarytools)
    
    ctable(mtcars$gear,mtcars$really_long_variable_name_for_vs,dnn = c('gear', 'vs'))
    
    #Cross-Tabulation, Row Proportions  
    #gear * vs  
    #Data Frame: mtcars  
    
    #------- ---- ------------ ------------ -------------
    #          vs            0            1         Total
    #   gear                                             
    #      3        12 (80.0%)    3 (20.0%)   15 (100.0%)
    #      4         2 (16.7%)   10 (83.3%)   12 (100.0%)
    #      5         4 (80.0%)    1 (20.0%)    5 (100.0%)
    #  Total        18 (56.2%)   14 (43.8%)   32 (100.0%)
    #------- ---- ------------ ------------ -------------