I'm looking for an implementation of +=
and -=
in R
. Anyone built that yet?
a = 1
a += 2 # 3
a -= 2 # -1
It does not exist in R default operators however you can manage to do something similar to it using roperators package as follows;
install.packages('roperators')
require(roperators)
# Assignment
a <- 1
print(a)
# [1] 1
# To incremenet value
a %+=% 2
print(a)
# [1] 3
# To decrement value
a %-=% 2
print(a)
# [1] 1
We could use inplace operators %+<-%
from inplace
library(inplace)
a <- 1
a %+<-% 2
-output
a
#[1] 3
Now, do the subtraction
a %-<-% 2
-output
a
#[1] 1
Or if we want to use the do this as it is, use the reticulate
to call python
library(reticulate)
py_run_string("a = 1")
py_run_string("a += 2")
py$a
#[1] 3