How to feed pipe into an inequality?

Viewed 170

This has come up in multiple instances, and I do not know if this current instance is generalizable to the many cases where this arises for me, but I'm hoping an answer may shed some light.

The simplest version of this is when I am doing some data processing and want to perform an evaluation on the result of a pipe. A simple example would be:

> seq(9) %>% > 4
Error: unexpected '>' in "seq(9) %>% >"
> seq(9) %>% . > 4
Error in .(.) : could not find function "."

The desired output would be a logical vector

FALSE FALSE FALSE FALSE TRUE TRUE TRUE TRUE TRUE

In many circumstances I want to evaluate on some piped output but have to assign it and then perform the evaluation for it to work:

seq(9) -> vec
vec > 4

Is there a way to complete these sorts of evaluations within a pipe chain entirely?

4 Answers

You need to use curly braces if you just want to use pipes.

seq(9) %>% {. > 4}

[1] FALSE FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE

I'd recommend using purrr if you're going to be piping these kinds of things, as it will result in a bit more readable code.

library(purrr)

map_lgl(seq(9), ~.x > 4)

[1] FALSE FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE

One more option is to call the > function as a function rather than an operator:

> seq(9) %>% `>`(4)
[1] FALSE FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE
> seq(9) %>% '>'(4)
[1] FALSE FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE

I think the backquotes make the most sense, but regular quotes do work in this case as well.

magrittr provides "aliases" to turn binary operators into pipe-able functions. I don't care for them myself, but you can do

library(magrittr)
seq(9) %>% is_greater_than(4)

See the full list at ?add (which is an alias for +). There are implementations for everything from + to [[.

In a slightly broader context, this is easily done with mutate:

library(dplyr)
data_frame(x=1:9) %>% mutate(big_x = x>4)

Unsolicited opinion: If you're mostly working with atomic vectors, I suspect that the tidyverse/piping approach is going to be more trouble than it's worth. As the other answer suggests, you can use purrr, but again, base R may work perfectly well. More context for your problem might help.

Related