You should pay more attention to Gabor Grothendeick (and not only in this case.) The above inc function on Matt Asher's blog does everything you ask:
(And the obvious extension works as well.)
add <- function(x, inc=1) { eval.parent(substitute(x <- x + inc)) }
EDIT: after my temporary annoyance in the absence of approval in the first comment, I took on the task of adding another function argument. Supplied with one argument to a part of the data frame, it would still increase the range of values by one. Up to this point, very little has been tested on infix-dyadic operators, but I see no reason why it should not work with any function that takes only two arguments:
transfn <- function(x, func="+", inc=1) { eval.parent(substitute(x <- do.call(func, list(x , inc)))) }
(Guilty input: it somehow “feels wrong” from the traditional R perspective to return values for the destination.) An earlier test of the inc function is below:
> df <- data.frame(a1 =1:10, a2=21:30, b=1:2) > inc <- function(x) { + eval.parent(substitute(x <- x + 1)) + } > inc(df$a1)
42-14 oct. 2018-11-14T00: 00Z
source share