ddply multiple quantiles by group

后端 未结 4 1112
野的像风
野的像风 2020-12-29 08:14

how can I do this calculation:

library(ddply)
quantile(baseball$ab)
  0%  25%  50%  75% 100% 
  0   25  131  435  705 

by groups, say by \"

4条回答
  •  天命终不由人
    2020-12-29 09:05

    A slightly different approach using dplyr:

    library(tidyverse)
    
    baseball %>% 
      group_by(team) %>% 
      nest() %>% 
      mutate(
        ret = map(data, ~quantile(.$ab, probs = c(0.25, 0.75))),
        ret = invoke_map(tibble, ret)
      ) %>%
      unnest(ret)
    

    Here you can specify the needed quantiles in the probs argument.

    The invoke_map call seems to be necessary, as quantile does not return a data frame; see this answer.

    You can also put that all into a function:

    get_quantiles <- function(.data, .var, .probs = c(0.25, 0.75), .group_vars = vars()) {
      .var = deparse(substitute(.var))
      return(
        .data %>% 
        group_by_at(.group_vars) %>% 
        nest() %>% 
        mutate(
          ret = map(data, ~quantile(.[[.var]], probs = .probs)),
          ret = invoke_map(tibble, ret)
        ) %>%
        unnest(ret, .drop = TRUE)
      )
    }
    
    mtcars %>% get_quantiles(wt, .group_vars = vars(cyl))
    

    A new approach would be to use group_modify() from dplyr. Then you'd call:

    baseball %>%
      group_by(team) %>% 
      group_modify(~{
        quantile(.x$ab, probs = c(0.25, 0.75)) %>% 
        tibble::enframe()
      }) %>%
      spread(name, value)
    

提交回复
热议问题