I have a vector vec like this vec <- c(1,4,5) and I want to check whether there are any steps down in the vector. I do this using:
any(diff(vec) < 0)
# [1] FALSE
No steps down. Okay. But when I try to use this code in a pipe I get a TRUE and an warning:
library(magrittr)
vec %>% diff() %>% any(. < 0)
# [1] TRUE
# Warning message:
# In any(., . < 0) : coercing argument of type 'double' to logical
How to correctly write any(diff(vec) < 0) as a pipe?
>Solution :
You can try
vec %>%
diff() %>%
`<`(0) %>%
any()
or
vec %>%
diff() %>%
{
. < 0
} %>%
any()
or
vec %>%
{
any(diff(.) < 0)
}