Why is this command with base R pipe and placeholder within dplyr::mutate returning an error message ?
library(dplyr)
iris[,-5] |> mutate(Row_sum = apply(X = _, 1, function(x) sum(x)))
#> Error in mutate(iris[, -5], Row_sum = apply(X = "_", 1, function(x) sum(x))) :
#> invalid use of pipe placeholder
Works fine outside mutate :
iris[,-5] |> apply(X = _, 1, function(x) sum(x))
#> [1] 10.2 9.5 9.4 9.4 10.2 11.4 9.7 10.1 8.9 9.6 10.8 10.0 9.3 8.5 11.2
#> [16] 12.0 11.0 10.3 11.5 10.7 10.7 10.7 9.4 10.6 10.3 9.8 10.4 10.4 10.2 9.7
#> [31] (...)
Works fine inside mutate with magrittr pipe
iris[,-5] %>% mutate(Row_sum = apply(X = ., 1, function(x) sum(x))) %>% head
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Row_sum
#> 1 5.1 3.5 1.4 0.2 10.2
#> 2 4.9 3.0 1.4 0.2 9.5
#> 3 4.7 3.2 1.3 0.2 9.4
#> 4 4.6 3.1 1.5 0.2 9.4
#> 5 5.0 3.6 1.4 0.2 10.2
#> 6 5.4 3.9 1.7 0.4 11.4