March 31, 2014

Why don't you use %>% ?

One of features of dplyr package, which is well known as very useful one for manipulation, is the operator %.%. It is called chain operator and chains any operation in R as follows.

library(dplyr)
iris %.% group_by(Species) %.% summarise(avg = mean(Sepal.Width))
## Source: local data frame [3 x 2]
## 
##      Species   avg
## 1     setosa 3.428
## 2 versicolor 2.770
## 3  virginica 2.974

%.% works like pipe operator in UNIX. %.% is simple but powerful. In case you have to make temporary objects, you don't need.

If you like F# style, you can use %>% operator with magrittr package. In the next version of dplyr, it is announced that %.% will be deprecated and replaced with %>%. Please check out below in detail.

https://groups.google.com/forum/#!msg/manipulatr/4EtIPVR3qEw/Xx4Vec7O0CQJ

library(magrittr)
iris %>% group_by(Species) %>% summarise(avg = mean(Sepal.Width))
## Source: local data frame [3 x 2]
## 
##      Species   avg
## 1     setosa 3.428
## 2 versicolor 2.770
## 3  virginica 2.974

No comments:

Post a Comment