Search code examples
rgtsummary

R, tbl_summary, treating continuous variables correctly


I am having a similar problem to what this user reported. Variables that are numeric and continuous are being treated as categorical. Lets just use the cars dataset as well so that we have something reproducible to work with. Lets say I simply do:

tbl_summary(mtcars)

enter image description here

Most of the variables will be treated as continuous but cyl, gear and carb for instance will be treated as categorical. I understand (per that other question) how to treat ALL of the variables as continuous, but what if I like most of my table and just want to change gear to be treated as continuous for instance? Is that possible?

In my real dataset, the variable I would like to be treated as continuous is already a numeric variable and I don't really see why its being treated as categorical, so I want to specify that gtsummary should treat that specific one as continuous.


Solution

  • According to ?tbl_summary, the type argument takes a named list. So, if we need the gear as 'continuous', specify that alone as a list in the type and it will be taken as that type while the other variables will be automatically judged

    library(gtsummary)
    tbl_summary(mtcars, type = list(gear ~ "continuous"))
    

    -output

    enter image description here