R: += (plus equals) and ++ (plus plus) equivalent from c++/c#/java, etc.?
R doesn't have a concept of increment operator
(as for example ++ in C). However, it is not difficult to implement one yourself, for example:
inc <- function(x)
{
eval.parent(substitute(x <- x + 1))
}
In that case you would call
x <- 10
inc(x)
However, it introduces function call overhead, so it's slower than typing x <- x + 1
yourself. If I'm not mistaken increment operator
was introduced to make job for compiler easier, as it could convert the code to those machine language instructions directly.
Following @GregaKešpret you can make an infix operator:
`%+=%` = function(e1,e2) eval.parent(substitute(e1 <- e1 + e2))
x = 1
x %+=% 2 ; x
No, it doesn't, see: R Language Definition: Operators