In R 4.1 introduced the native forward pipe (|>
) that can be used instead of magrittr's %>%
. For example,
c(1, 2, 3) |> length()
is a valid syntax in R > 4.1 and will return 3.
magrittr has a collection of aliases. For example, instead of
1 * 2
I can write
1 %>% magrittr::multiply_by(2)
What are the R > 4.1 equivalent to magrittr's aliases?
CodePudding user response:
Such aliases are not provided by base but this works to multiply by 2. The parentheses shown are required.
1:3 |> (`*`)(2)
It is also possible to use magrittr aliases with |>
library(magrittr)
1:3 |> multiply_by(2)
or to define an alias yourself
mult <- function(x, y) x * y
1:3 |> mult(2)
or
mult <- `*`
1:3 |> mult(2)
Another possibility is to find another way of expressing the same calculation. In this case:
1:3 |> sapply(`*`, 2)