如何将 any(diff(vec) < 0) 以管道形式表示?

huangapple go评论95阅读模式
英文:

How to write any(diff(vec) < 0) as a pipe?

问题

你可以将 any(diff(vec) < 0) 写成管道操作如下:

  1. vec %>%
  2. diff() %>%
  3. `<`(0) %>%
  4. any()

这将产生相同的结果而避免警告。

英文:

I have a vector vec like this vec &lt;- c(1,4,5) and I want to check whether there are any steps down in the vector. I do this using:

  1. any(diff(vec) &lt; 0)
  2. # [1] FALSE

No steps down. Okay. But when I try to use this code in a pipe I get a TRUE and an warning:

  1. library(magrittr)
  2. vec %&gt;% diff() %&gt;% any(. &lt; 0)
  3. # [1] TRUE
  4. # Warning message:
  5. # In any(., . &lt; 0) : coercing argument of type &#39;double&#39; to logical

How to correctly write any(diff(vec) &lt; 0) as a pipe?

答案1

得分: 2

你可以尝试:

  1. vec %>%
  2. diff() %>%
  3. `<`(0) %>%
  4. any()

或者

  1. vec %>%
  2. diff() %>%
  3. {
  4. . < 0
  5. } %>%
  6. any()

或者

  1. vec %>%
  2. {
  3. any(diff(.) < 0)
  4. }
英文:

You can try

  1. vec %&gt;%
  2. diff() %&gt;%
  3. `&lt;`(0) %&gt;%
  4. any()

or

  1. vec %&gt;%
  2. diff() %&gt;%
  3. {
  4. . &lt; 0
  5. } %&gt;%
  6. any()

or

  1. vec %&gt;%
  2. {
  3. any(diff(.) &lt; 0)
  4. }

答案2

得分: 2

你已经有一些解决方案了,但没有解释。原始代码无法正常工作的原因是magrittr管道在嵌套函数调用中对.的处理方式不同。

所谓嵌套函数调用是指.不是管道目标的函数的参数。在any(. &lt; 0)中,该函数是any(),其参数是. &lt; 0,实际上是对&lt;函数的函数调用。因此,.位于“嵌套函数调用”中。

如果.只出现在嵌套函数调用中,它会被隐式地用作第一个参数,所以代码

  1. x %&gt;% any(. &lt; 0)

等同于

  1. any(x, x &lt; 0)

为了抑制这种奇怪的行为,将表达式放在大括号中,即你的示例将变为

  1. vec %&gt;% diff() %&gt;% { any(. &lt; 0) }
英文:

You have a couple of solutions already, but no explanation. The reason your original didn't work is that magrittr pipes treat . differently when it is in a nested function call.

By nested function call, I mean that . is not an argument to the function that is the target of the pipe. In any(. &lt; 0), that function is any(), and its argument is . &lt; 0, which is actually a function call to the &lt; function. So the . is in a "nested function call".

If . is only in a nested function call, it is implicitly used as the first argument as well, so the code

  1. x %&gt;% any(. &lt; 0)

is treated as

  1. any(x, x &lt; 0)

To suppress this weird behaviour, put the expression in braces, i.e. your example would be

  1. vec %&gt;% diff() %&gt;% { any(. &lt; 0) }

答案3

得分: 1

使用magrittr管道:

  1. vec %>% diff() %>% `<(0)` %>% any()
  2. [1] FALSE

使用基本管道:

  1. vec |> diff() |> {(x)x < 0}() |> any()
  2. [1] FALSE

更多信息请参考这里:
https://stackoverflow.com/a/72474832/16730940

英文:

using magrittr pipes:

  1. vec %&gt;% diff() %&gt;% `&lt;`(0) %&gt;% any()
  2. [1] FALSE

Using base pipes:

  1. vec |&gt; diff() |&gt; {\(x)x &lt; 0}() |&gt; any()
  2. [1] FALSE

See here for more info:
https://stackoverflow.com/a/72474832/16730940

答案4

得分: 1

Base

  1. vec |&gt; diff() |&gt; base::`&lt;`(0) |&gt; any()
  2. # [1] FALSE

magrittr

  1. library(magrittr)
  2. vec %&gt;% diff %&gt;% `&lt;`(0) %&gt;% any
  3. # [1] FALSE

或者,如 @MrFlick 建议的:

  1. library(magrittr)
  2. vec %&gt;% diff %&gt;% is_less_than(0) %&gt;% any
  3. # [1] FALSE

数据:

  1. vec &lt;- c(1, 4, 5)
英文:

Base

  1. vec |&gt; diff() |&gt; base::`&lt;`(0) |&gt; any()
  2. # [1] FALSE

magrittr

  1. library(magrittr)
  2. vec %&gt;% diff %&gt;% `&lt;`(0) %&gt;% any
  3. # [1] FALSE

or, as suggested by @MrFlick

  1. library(magrittr)
  2. vec %&gt;% diff %&gt;% is_less_than(0) %&gt;% any
  3. # [1] FALSE

Data:

  1. vec &lt;- c(1, 4, 5)

huangapple
  • 本文由 发表于 2023年6月26日 15:33:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76554465.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定