Writing Your Own Binary Operations

You can invent your own operations! Just write a function whose name begins and ends with %, with two arguments of a certain type, and a return value of that type.

For example, here’s a binary operation that adds double the second operand to the first:

> "%a2b%" <- function(a,b) return(a+2*b)
> 3 %a2b% 5
[1] 13

A less trivial example is given in the section about set operations in Section 8.5.

Get The Art of R Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.