【问题标题】:How can I pass a value to the `filter` function with a pipe without the curly brackets?如何使用不带大括号的管道将值传递给“过滤器”函数?
【发布时间】:2022-10-14 20:43:17
【问题描述】:

如何在不使用大括号的情况下使用管道将值传递给 filter 函数?

library(dplyr)
4 %>% {filter(mtcars, cyl == .)} # Works
4 %>% filter(mtcars, cyl == .) # Does not work
Error in UseMethod("filter") : 
  no applicable method for 'filter' applied to an object of class "c('double', 'numeric')"

【问题讨论】:

  • 你的问题是什么?您发布的文字已经回答了标题中的问题。
  • 我认为他想在没有花括号的情况下做到这一点。
  • @KonradRudolph确实,我希望它没有大括号。标题已编辑

标签: r dplyr pipe


【解决方案1】:

你不能。

“magrittr”管道的语义是这样的,即 LHS 作为 RHS 的第一个参数插入除非. 用于参数的顶层。那是,

x %>% f(...)

总是相当于

f(x, ...)

除非... 在顶层包含.

为了抑制这种行为,%>% 专门提供了您已经知道的 {…} 语法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-15
    • 2021-11-22
    • 2012-07-15
    • 1970-01-01
    • 1970-01-01
    • 2020-12-04
    相关资源
    最近更新 更多