相关疑难解决方法(0)

%>% .$column_name 等效于 R 基管 |>

我经常使用 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 dplyr

20
推荐指数
4
解决办法
787
查看次数

R 的新原生管道 `|>` 和 magrittr 管道 `%>%` 有什么区别?

在 R 4.1 中引入了一个本地管道运算符,它比以前的实现“更加精简”。我已经注意到 native|>和 magrittr pipe之间的一个区别%>%,即2 %>% sqrt可以工作但2 |> sqrt不能,并且必须写为2 |> sqrt(). 使用新的管道运算符时是否有更多差异和陷阱需要注意?

r pipe tidyverse

12
推荐指数
5
解决办法
671
查看次数

在条件 dplyr 链中使用基本 R 管道

我想知道在 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)

r

4
推荐指数
1
解决办法
245
查看次数

标签 统计

r ×3

dplyr ×1

pipe ×1

tidyverse ×1