What does the %<>% operator mean in R? [closed]

牧云@^-^@ 提交于 2019-12-01 08:29:01

问题


  1. What does the %<>% operator do in R ?
  2. What's the difference between using %<>% and <- ?
  3. In what type of circumstances %<>% could be useful ?

回答1:


The help, ?magrittr::`%<>%`, answers all your questions, if you are refering to magrittr`s compound assignment pipe-operator:

[...] %<>% is used to update a value by first piping it into one or more rhs expressions, and then assigning the result. For example, some_object %<>% foo %>% bar is equivalent to some_object <- some_object %>% foo %>% bar. It must be the first pipe-operator in a chain, but otherwise it works like %>%.

So

library(magrittr)
set.seed(1);x <- rnorm(5)
x %<>% abs %>% sort
x
# [1] 0.1836433 0.3295078 0.6264538 0.8356286 1.5952808

is the same as

set.seed(1);x <- rnorm(5)
x <- sort(abs(x))
x
# [1] 0.1836433 0.3295078 0.6264538 0.8356286 1.5952808


来源:https://stackoverflow.com/questions/37772861/what-does-the-operator-mean-in-r

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