This is the dataframe I created using Iris dataset.
library(datasets)
df = iris[which(iris$Species == "setosa" | iris$Species == "versicolor"),]
# Success
df %>% t.test(Sepal.Length ~ Species, data = .)
# Error is.atomic(x) is not TRUE
df |> t.test(Sepal.Length ~ Species, data = .)
I wonder why they did not work the same and when will %>% and |> be uninterchangeable?
In the second approach, you use
., which is themagrittrplaceholder when you should use_(which is the native pipe's placeholder).This does work:
You can find out much more about the differences beyond the scope of this question here: What are the differences between R's new native pipe `|>` and the magrittr pipe `%>%`?