简体   繁体   English

我如何“magrittr”管道到 R 包“Hmisc”的 summarise() 函数?

[英]How do I 'magrittr' pipe to the summarize() function of the R package 'Hmisc'?

I am trying to reduce the nested parenthesis using pipes of package magrittr .我正在尝试使用包magrittr的管道来减少嵌套括号。 So I am trying to implement summarize() function of Hmisc package.所以我正在尝试实现Hmisc包的summarize()函数。

 data(iris)
summarize(iris$Sepal.Length,iris$Species,mean)

How to use pipes (%>%) of magrittr package in R?如何在 R 中使用magrittr包的管道(%>%)

Hello you can use with like this :您好,您可以像这样with

library("Hmisc")
library("magrittr")
data(iris)
iris %>% with(summarize(X = Sepal.Length, by = Species, FUN = mean))

EDIT : Even better you can use %$% operator and skip the with step :编辑:更好的是,您可以使用%$%运算符并跳过with步骤:

iris %$% summarize(X = Sepal.Length, by = Species, FUN = mean)

##     Species Sepal.Length
##1     setosa        5.006
##2 versicolor        5.936
##3  virginica        6.588

You can use the dplyr package.您可以使用dplyr包。

> library("dplyr")
> iris %>% group_by(Species) %>% summarize(length = mean(Sepal.Length))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM