我经常使用 dplyr 管道将一列从 tibble 转换为向量,如下所示
iris %>% .$Sepal.Length
iris %>% .$Sepal.Length %>% cut(5)
Run Code Online (Sandbox Code Playgroud)
如何使用最新的 R 内置管道符号执行相同操作 |>
iris |> .$Sepal.Length
iris |> .$Sepal.Length |> cut(5)
Error: function '$' not supported in RHS call of a pipe
Run Code Online (Sandbox Code Playgroud) 在 R 4.1 中引入了一个本地管道运算符,它比以前的实现“更加精简”。我已经注意到 native|>和 magrittr pipe之间的一个区别%>%,即2 %>% sqrt可以工作但2 |> sqrt不能,并且必须写为2 |> sqrt(). 使用新的管道运算符时是否有更多差异和陷阱需要注意?
我想知道在 dplyr 链存在条件元素的情况下如何使用基管|>代替 tidyverse 的基管。%>%当我尝试使用以下代码时|>,出现错误:
Error: function '{' not supported in RHS call of a pipe
Run Code Online (Sandbox Code Playgroud)
Tidyverse 示例
library(tidyverse)
condition = FALSE
mtcars %>%
{ if(condition == TRUE)
mutate(., mpg = mpg*1000)
else . }
Run Code Online (Sandbox Code Playgroud)
基本 R 管道示例(导致错误):
mtcars |>
{ if(condition == TRUE)
mutate(., mpg = mpg*1000)
else . }
Run Code Online (Sandbox Code Playgroud)