Search code examples
rdplyrhmisc

Can I pipe into describe in R?


Describe from Hmisc is one of my favorite functions. It gives a great looking summary for a dataset.

I'd like to be able to filter a dataset with dplyr and then pass a single column to describe().

Something like this

mtcars %>% filter(cyl > 5) %>% describe(cyl)

Solution

  • You could use select :

    library(dplyr)
    library(Hmisc)
    
    mtcars %>% filter(cyl > 5) %>% select(cyl) %>% describe()
    
    1  Variables      21  Observations
    ----------------------------------------------------------------------------------
    cyl 
           n  missing distinct     Info     Mean      Gmd 
          21        0        2    0.668    7.333   0.9333 
                          
    Value          6     8
    Frequency      7    14
    Proportion 0.333 0.667
    ----------------------------------------------------------------------------------