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

后端 未结 3 815
[愿得一人]
[愿得一人] 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:42

    You can achieve a similar output combining the dplyr and tidyr packages. Something along these lines can help

    library(dplyr)
    library(tidyr)
    
    iris %>%
      select(matches("Petal")) %>%
      summarise_each(funs(min, max)) %>%
      gather(variable, value) %>%
      separate(variable, c("var", "stat"), sep = "\\_") %>%
      spread(var, value)
    ##   stat Petal.Length Petal.Width
    ## 1  max          6.9         2.5
    ## 2  min          1.0         0.1
    

提交回复
热议问题