本文介绍了使用 dplyr 的 summarise_each 为每个函数返回一行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 dplyr 的 summarise_each 将函数应用于多列数据.一件好事是您可以一次应用多个功能.问题是,输出是一个单行的数据帧,这很烦人.似乎它应该返回与函数一样多的行,列与汇总的列一样多.

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's annoying that the output is a dataframe with a single row. It seems like it should return as many rows as functions, with as many columns as columns that were summarised.

library(dplyr)  
default <- 
  iris %>% 
  summarise_each(funs(min, max), matches("Petal"))

返回

> default
  Petal.Length_min Petal.Width_min Petal.Length_max Petal.Width_max
1                1             0.1              6.9             2.5

我更喜欢类似的东西

library(reshape2)
desired <- 
  iris %>% 
  select(matches("Petal")) %>% 
  melt() %>% 
  group_by(variable) %>% 
  summarize(min=min(value),max=max(value)) %>%
  t()

返回一些接近的东西(不是数据帧,但你们都明白了)

which returns something close (not a dataframe, but you all get the idea)

> desired
         [,1]           [,2]         
variable "Petal.Length" "Petal.Width"
min      "1.0"          "0.1"        
max      "6.9"          "2.5" 

summarise_each 中是否有选项可以执行此操作?如果没有,哈德利,你介意添加吗?

is there an option in summarise_each to do this? If not, Hadley, would you mind adding it?

推荐答案

结合 dplyrtidyr 包,您可以获得类似的输出.这些方面的东西可以提供帮助

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

这篇关于使用 dplyr 的 summarise_each 为每个函数返回一行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 17:05