R: += (plus equals) and ++ (plus plus) equivalent from c++/c#/java, etc.?

前端 未结 9 809
攒了一身酷
攒了一身酷 2020-11-29 00:40

Does R have a concept of += (plus equals) or ++ (plus plus) as c++/c#/others do?

9条回答
  •  感情败类
    2020-11-29 01:17

    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.

提交回复
热议问题