使用 magrittr 和 which
Using magrittr and which
我想用 magrittr 的管道重写以下代码:
max(diff(which(diff(runif(50)) > 0 )))
我的直接方法是:
library(magrittr)
runif(50) %>% diff > 0 %>% which %>% diff %>% max
但是由于(第一个)which:
runif(50) %>% diff > 0 %>% which
其中 (.) 的错误:'which' 的参数不符合逻辑
我不确定为什么会出现此错误,以及为什么管道似乎不同于管道到其他函数,因为“diff > 0”的输出是一个逻辑向量。
顺便说一句,有没有办法通过管道进行比较,说白了
runif(50) %>% diff %>% > 0
尝试:
runif(50) %>% diff %>% `>`(0) %>% which %>% diff %>% max
编辑:可能应该指出那些是反引号,而不是引号。
我相信你的问题字符串被解析为等同于:
(runif(50) %>% diff) > (0 %>% which %>% diff %>% max)
它returns同样的错误。
另一方面,除了已经提出的建议之外,这些还可以正常工作:
runif(50) %>% {diff(.) > 0} %>% which %>% diff %>% max
(runif(50) %>% diff > 0) %>% which %>% diff %>% max
我想用 magrittr 的管道重写以下代码:
max(diff(which(diff(runif(50)) > 0 )))
我的直接方法是:
library(magrittr)
runif(50) %>% diff > 0 %>% which %>% diff %>% max
但是由于(第一个)which:
runif(50) %>% diff > 0 %>% which
其中 (.) 的错误:'which' 的参数不符合逻辑
我不确定为什么会出现此错误,以及为什么管道似乎不同于管道到其他函数,因为“diff > 0”的输出是一个逻辑向量。
顺便说一句,有没有办法通过管道进行比较,说白了
runif(50) %>% diff %>% > 0
尝试:
runif(50) %>% diff %>% `>`(0) %>% which %>% diff %>% max
编辑:可能应该指出那些是反引号,而不是引号。
我相信你的问题字符串被解析为等同于:
(runif(50) %>% diff) > (0 %>% which %>% diff %>% max)
它returns同样的错误。
另一方面,除了已经提出的建议之外,这些还可以正常工作:
runif(50) %>% {diff(.) > 0} %>% which %>% diff %>% max
(runif(50) %>% diff > 0) %>% which %>% diff %>% max