Skip to content

Instantly share code, notes, and snippets.

@kevinushey
Last active August 29, 2015 13:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevinushey/9266383 to your computer and use it in GitHub Desktop.
Save kevinushey/9266383 to your computer and use it in GitHub Desktop.
Overloading `|` to act like a pipe operator for data.frames
`|` <- function(x, y) {
if (is.data.frame(x)) {
return( eval(call("%.%", substitute(x), substitute(y)), envir=parent.frame()) )
} else {
return( base::"|"(x, y) )
}
}
library(dplyr)
mtcars |
arrange(cyl, disp) |
mutate(cyl = disp + hp * cyl) |
mutate(cyl = cyl * 2)
c(TRUE, FALSE) | c(FALSE, TRUE)
df <- data.frame(x=TRUE, y=FALSE)
df |
mutate(z = x | y)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment