dplyrでcolwise
dplyrを使って,plyrのcolwiseのような処理をしたい.このようなときはHadley Wickham氏がgithubで公開しているdplyrパッケージのsummarise_each関数を使用すればよい.
> library(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 Petal.Width_min 1 setosa 4.3 2.3 1.0 0.1 2 versicolor 4.9 2.0 3.0 1.0 3 virginica 4.9 2.2 4.5 1.4 Variables not shown: Sepal.Length_max (dbl), Sepal.Width_max (dbl), Petal.Length_max (dbl), Petal.Width_max (dbl)
この機能はdplyr-0.2から導入予定とのこと.
参考:
How can I use dplyr to apply a function to all non-group_by columns?