R 中的 %<>% 运算符是什么意思?

What does the %<>% operator mean in R?

  1. %<>% 运算符在 R 中有什么作用?
  2. 使用 %<>%<- 有什么区别?
  3. 在什么情况下 %<>% 有用?

如果您指的是 magrittr 的复合赋值管道运算符,帮助 ?magrittr::`%<>%` 会回答您的所有问题:

[...] %<>% 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 %>%.

所以

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

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