incr {refer} | R Documentation |
Increment Value In Place
Description
Increase the value of an object on the search path. Equivalent to x++
or x += by
in other languages.
Usage
incr(x, by = 1)
Arguments
x |
object to be incremented; can be a symbol, character, or extraction language object. |
by |
value to increase |
Details
incr
quotes object x
, then attempts to determine the primary object to be modified.
For example, z
will be the 'primary object' in incr(z[1:4])
. incr
then searches
for the primary object in the search path and records the environment. x <- x + by
is then
evaluated within the recorded environment.
The quoted object can be a symbol or character object. It can also be language object, though the primary
call must be either `$`
, `[`
, or `[[`
. These can be nested. For example, x[1]
or x[2, 1][3]
is acceptable, but sqrt(x)
is not.
See decr
to decrease the value.
Value
the value of x
incremented by by
, invisibly
Examples
z <- 1:10
incr(z)
identical(z, as.numeric(2:11)) # TRUE
incr(z[1:3], by=2)
identical(z[1:3], as.numeric(4:6)) # TRUE
l <- list(a = 1, b = 2)
decr(l$a)
l$a == 0 # TRUE
decr(l$b, by = 4)
l$b == -2 # TRUE