magrittr - What does %>% mean in R -


i following example, server.r file here,

https://github.com/wch/movies/blob/master/server.r

i plan similar filter, lost %>% does.

# apply filters m <- all_movies %>%   filter(     reviews >= reviews,     oscars >= oscars,     year >= minyear,     year <= maxyear,     boxoffice >= minboxoffice,     boxoffice <= maxboxoffice   ) %>% 

the infix operator %>% not part of base r, in fact defined package magrittr (cran) , heavily used dplyr (cran).

it works pipe, hence reference magritte's famous painting la trahison des images.

what function pass lhs first argument of rhs. in following example, data frame iris gets passed head():

library(magrittr) iris %>% head()   sepal.length sepal.width petal.length petal.width species 1          5.1         3.5          1.4         0.2  setosa 2          4.9         3.0          1.4         0.2  setosa 3          4.7         3.2          1.3         0.2  setosa 4          4.6         3.1          1.5         0.2  setosa 5          5.0         3.6          1.4         0.2  setosa 6          5.4         3.9          1.7         0.4  setosa 

thus, iris %>% head() equivalent head(iris).

often, %>% called multiple times "chain" functions together, accomplishes same result nesting. example in chain below, iris passed head(), result of passed summary().

iris %>% head() %>% summary() 

thus iris %>% head() %>% summary() equivalent summary(head(iris)). people prefer chaining nesting because functions applied can read left right rather inside out.


Comments

Popular posts from this blog

c++ - No viable overloaded operator for references a map -

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - Cannot secure connection using TLS -