Snippets

Stuart Morrison Compounded arithmetic operators in R

Created by Stuart Morrison
# Compounded addition
`%+%` <- function(x, y) {
    eval.parent(substitute(x <- x + y))
}    

# Compounded subtraction
`%-%` <- function(x, y) {
    eval.parent(substitute(x <- x - y))
}    

# Test in a loop
x <- 10
y <- 10
for (i in 1:10){
    x %+% i
    y %-% i
}
x
y

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.