如何使用dplyr将函数应用于所有非group_by列?

时间:2022-09-23 19:22:47

I'm trying to use the dplyr package to apply a function to all columns in a data.frame that are not being grouped, which I would do with aggregate():

我正在尝试使用dplyr包将函数应用于未被分组的data.frame中的所有列,我将使用aggregate():

aggregate(. ~ Species, data = iris, mean)

where mean is applied to all columns not used for grouping. (Yes, I know I can use aggregate, but I'm trying to understand dplyr.)

其中mean适用于所有不用于分组的列。 (是的,我知道我可以使用聚合,但我正在尝试理解dplyr。)

I can use summarize like this:

我可以使用这样的总结:

species <- group_by(iris, Species)
summarize(species,
          Sepal.Length = mean(Sepal.Length),
          Sepal.Width = mean(Sepal.Width))

But is there a way to have mean() applied to all columns that are not grouped, similar to the . ~ notation of aggregate()? I have a data.frame with 30 columns that I want to aggregate, so writing out the individual statements is not ideal.

但有没有办法让mean()应用于所有未分组的列,类似于。 〜aggregate()的表示法?我有一个我想要聚合的30列data.frame,所以写出单个语句并不理想。

2 个解决方案

#1


34  

If you're willing to try out an experimental dplyr, you can try out the new (and still experimental) summarise_each():

如果你愿意尝试一个实验性的dplyr,你可以尝试新的(并且仍然是实验性的)summarise_each():

devtools::install_github("hadley/dplyr", ref = "colwise")

library(dplyr)
iris %.%
  group_by(Species) %.%
  summarise_each(funs(mean))
## Source: local data frame [3 x 5]
## 
##      Species Sepal.Length Sepal.Width Petal.Length Petal.Width
## 1     setosa        5.006       3.428        1.462       0.246
## 2 versicolor        5.936       2.770        4.260       1.326
## 3  virginica        6.588       2.974        5.552       2.026

iris %.%
  group_by(Species) %.%
  summarise_each(funs(min, max))
## Source: local data frame [3 x 9]
## 
##      Species Sepal.Length_min Sepal.Width_min Petal.Length_min
## 1     setosa              4.3             2.3              1.0
## 2 versicolor              4.9             2.0              3.0
## 3  virginica              4.9             2.2              4.5
## Variables not shown: Petal.Width_min (dbl), Sepal.Length_max (dbl),
##   Sepal.Width_max (dbl), Petal.Length_max (dbl), Petal.Width_max (dbl)

Feedback much appreciated!

反馈非常感谢!

This will appear in dplyr 0.2.

这将出现在dplyr 0.2中。

#2


4  

This will get you almost all the way in dplyr.

这将在dplyr中几乎一路走来。

h = iris %.%
  group_by(Species) %.%
  do(function(d){
    sapply(Filter(is.numeric, d), mean)  
  })

as.data.frame(h)

#1


34  

If you're willing to try out an experimental dplyr, you can try out the new (and still experimental) summarise_each():

如果你愿意尝试一个实验性的dplyr,你可以尝试新的(并且仍然是实验性的)summarise_each():

devtools::install_github("hadley/dplyr", ref = "colwise")

library(dplyr)
iris %.%
  group_by(Species) %.%
  summarise_each(funs(mean))
## Source: local data frame [3 x 5]
## 
##      Species Sepal.Length Sepal.Width Petal.Length Petal.Width
## 1     setosa        5.006       3.428        1.462       0.246
## 2 versicolor        5.936       2.770        4.260       1.326
## 3  virginica        6.588       2.974        5.552       2.026

iris %.%
  group_by(Species) %.%
  summarise_each(funs(min, max))
## Source: local data frame [3 x 9]
## 
##      Species Sepal.Length_min Sepal.Width_min Petal.Length_min
## 1     setosa              4.3             2.3              1.0
## 2 versicolor              4.9             2.0              3.0
## 3  virginica              4.9             2.2              4.5
## Variables not shown: Petal.Width_min (dbl), Sepal.Length_max (dbl),
##   Sepal.Width_max (dbl), Petal.Length_max (dbl), Petal.Width_max (dbl)

Feedback much appreciated!

反馈非常感谢!

This will appear in dplyr 0.2.

这将出现在dplyr 0.2中。

#2


4  

This will get you almost all the way in dplyr.

这将在dplyr中几乎一路走来。

h = iris %.%
  group_by(Species) %.%
  do(function(d){
    sapply(Filter(is.numeric, d), mean)  
  })

as.data.frame(h)