简体   繁体   中英

Differences between %.% (dplyr) and %>% (magrittr)

The dplyr package introduced the %.% operator to pass the left hand side as an argument of the function on the right hand side, similar to a *NIX pipe. The magrittr package is a much more lightweight package that exists to define only that pipe-like operator.

Yet one uses %.% and the other %>% .

Is there any difference between the two? Can I just use %>% even in dplyr code, or will that cause subtle bugs?

On inspecting the code, they take very different paths early on, so simple eyeball comparison would suggest that they're different. I haven't been able to find anything documented when I search the web for them, nor have I run across anything in the documentation itself.

See the very first item in the current NEWS file:

dplyr now imports %>% from magrittr (#330). I recommend that you use this instead of %.% because it is easier to type (since you can hold down the shift key) and is more flexible

dplyr now imports %>% from magrittr and uses it by default. See this answer for details.


Differences include

  • you can use a . as placeholder for the left-hand side, eg

     iris %>% plot(Sepal.Length ~ Sepal.Width, data = .)
  • %>% respects (rhs) , eg

     1:10 %>% (call("sum")) 1:10 %>% (function(x) x^2 + 2*x)

    For a more useful example of this, see
    https://gist.github.com/anonymous/0c69b019d0b4f6ae5050

  • For single argument function calls, you can omit parens:

     "2014-05-18" %>% as.Date

I hate to say this but the %>% should be banned. No one or nothing describes what it actually does and it seems that it changes. We should not have crap like this as it is too confusing. Every explanation i keep reading contradicts or makes no sense. Can we just get rid of this operator or whatever it is? I use R a lot and find every other function or operator as self explanatory. This is garbage and just makes it unusable until someone can either explain it clearly or standardize it. Best just to get rid of it until then.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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