incr: Increment Value In Place

Description Usage Arguments Details Value Examples

View source: R/incr.R

Description

Increase the value of an object on the search path. Equivalent to x++ or x += by in other languages.

Usage

1
incr(x, by = 1)

Arguments

x

object to be incremented; can be a symbol, character, or extraction language object.

by

value to increase x by; defaults to 1.

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
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

refer documentation built on Nov. 8, 2021, 5:08 p.m.