This is basically the same question as Chain arithmetic operators in dplyr with %>% pipe but updated for the new (as R 4.1) pipe operator |>
.
How can I chain arithmetic operators with the R native pipe |>
? With dplyr/magrittr, you can use backticks for arithmetic operators, but that doesn't work with the inbuilt R pipe operator. Here's a simple example:
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
Hopefully, the answer would be generic enough to work for any operator or in-built function that does not usually work nicely with the native R pipe (my version is R 4.2.2).
The answer https://stackoverflow.com/a/72086492/2449926 has lots of useful information on the differences between %>%
and |>
, but none that quite answers my question.