这基本上是与Chain arithmetic operators in dplyr with %>% pipe相同的问题,但针对新的(如R 4.1)管道操作员|>
进行了更新.
How can I chain arithmetic operators with the R native pipe 100?使用dplyr/magrittr,您可以对算术运算符使用反号,但这不适用于内置的R管道运算符.下面是一个简单的例子:
R.version$version.string
# [1] "R version 4.2.2 (2022-10-31 ucrt)"
x <- 2
# With dplyr/magrittr, you can use backticks for arithmetic operators
x %>% `+`(2)
# [1] 4
# But that doesn't work with the inbuilt R pipe operator
x |> `+`(2)
# Error: function '+' not supported in RHS call of a pipe
希望答案足够通用,以适用于任何运算符或内置函数,这些运算符或内置函数通常不能很好地与本机R管道一起工作(我的版本是R 4.2.2).
答案https://stackoverflow.com/a/72086492/2449926有很多关于%>%
和|>
之间区别的有用信息,但没有一个能很好地回答我的问题.