use dplyr's summarise_each to return one row per function?

后端 未结 3 819
[愿得一人]
[愿得一人] 2020-12-13 04:43

I\'m using dplyr\'s summarise_each to apply a function to multiple columns of data. One thing that\'s nice is that you can apply multiple functions at once. Thing is, it\'

3条回答
  •  不思量自难忘°
    2020-12-13 05:34

    One option is to use purrr::map_df (really map_dfc to simplify back to a data.frame with bind_cols though map_df is fine for now) with a function that makes a vector of results of each function, i.e.

    library(tidyverse)
    
    iris %>% select(contains('Petal')) %>% 
        map_dfc(~c(min(.x), max(.x))) %>% 
        mutate(stat = c('min', 'max'))    # to add column of function names
    
    #> # A tibble: 2 × 3
    #>   Petal.Length Petal.Width  stat
    #>                  
    #> 1          1.0         0.1   min
    #> 2          6.9         2.5   max
    

提交回复
热议问题