Skip to contents

The x %:=% f operator performs in-place modification of some object x with a function f.

For example this:

mtcars$mpg[mtcars$cyl > 6] <- mtcars$mpg[mtcars$cyl>6]^2

Can now be re-written as:

mtcars$mpg[mtcars$cyl > 6] %:=% \(x) x^2

Usage

x %:=% f

Arguments

x

a variable.

f

a (possibly anonymous) function to be applied in-place on x. The function must take one argument only.

Value

This operator does not return any value:

It is an in-place modifier, and thus modifies the object directly.


See also

Examples

set.seed(1)
object <- matrix(rpois(10, 10), ncol = 2)
print(object)
#>      [,1] [,2]
#> [1,]    8   12
#> [2,]   10   11
#> [3,]    7    9
#> [4,]   11   14
#> [5,]   14   11
y <- 3
object %:=% \(x) x + y # same as object <- object + y
print(object)
#>      [,1] [,2]
#> [1,]   11   15
#> [2,]   13   14
#> [3,]   10   12
#> [4,]   14   17
#> [5,]   17   14