Search code examples
rdplyrsummarize

Why does summarize function don't get an error?


We all know that we can compute any summary that operates on vectors and returns a single value in R base. I want to ask that why I don't get an error when I attempt to use quantile which returns more than one value function inside of summarize?

heights %>%
    filter(sex == "Male") %>%
    summarize(range = quantile(height, c(0, 0.5, 1)))

result is not an error:

     range
1 50.00000
2 69.00000
3 82.67717

Solution

  • Just making my comment and @27ϕ9's an answer.

    As of dplyr version 1.0.0, summarise allows to return outputs containing more than one value.

    Example:

    mtcars %>%
      group_by(cyl) %>%
      summarise(qs = quantile(disp, c(0.25, 0.75)), prob = c(0.25, 0.75))
    
    # A tibble: 6 x 3
    # Groups:   cyl [3]
    #     cyl    qs  prob
    #   <dbl> <dbl> <dbl>
    # 1     4  78.8  0.25
    # 2     4 121.   0.75
    # 3     6 160    0.25
    # 4     6 196.   0.75
    # 5     8 302.   0.25
    # 6     8 390    0.75
    

    See the documentation for more examples.