What is the difference between %>% and %,% in magrittr?

夙愿已清 提交于 2019-11-29 02:33:09

问题


Github developmental version of magrittr includes some cool new function for piping but I do not exactly catch de difference between %>% and %,%. Is this only formal with %>% for value and %,% for functions, or there is some specific peculiarity?


回答1:


The normal piping operator is %>%. You can use %,% to create a reusable pipe, a pipe without data. Then later you can use the same pipe with various data sets. Here is an example.

library(magrittr)
library(dplyr)
library(Lahman)

Suppose you want to calculate the top 5 baseball players, according to total hits. Then you can do something like this (taken from the magrittr README):

Batting %>%
   group_by(playerID) %>%
   summarise(total = sum(G)) %>%
   arrange(desc(total)) %>%
   head(5)
# Source: local data frame [5 x 2]
# 
#    playerID total
# 1  rosepe01  3562
# 2 yastrca01  3308
# 3 aaronha01  3298
# 4 henderi01  3081
# 5  cobbty01  3035

So far so good. Now let's assume that you have several data sets in the same format as Batting, so you could just reuse the same pipe again. %,% helps you create, save and reuse the pipe:

top_total <- group_by(playerID) %,%
   summarise(total = sum(G)) %,%
   arrange(desc(total)) %,%
   head(5)

top_total(Batting)
# Source: local data frame [5 x 2]
# 
#    playerID total
# 1  rosepe01  3562
# 2 yastrca01  3308
# 3 aaronha01  3298
# 4 henderi01  3081
# 5  cobbty01  3035

Of course you could also create a function the regular R way, i.e. top_total <- function(...) ..., but %,% is a more concise way.



来源:https://stackoverflow.com/questions/25211465/what-is-the-difference-between-and-in-magrittr

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!