assign_ops: Assignment operators

assign_opsR Documentation

Assignment operators

Description

Modifies the stored value of the left-hand-side object by the right-hand-side object. Equivalent of operators such as += -= *= /= in languages like c++ or python. %+=% and %-=% can also work with strings.

Usage

x %+=% y

x %-=% y

x %*=% y

x %/=% y

x %^=% y

x %log=% y

x %root=% y

Arguments

x

a stored value

y

value to modify stored value by

Author(s)

Ben Wiseman, benjamin.wiseman@kornferry.com

Examples

x <- 1

x %+=% 2

x == 3 # TRUE

x %-=% 3

x == 0 # TRUE

# Or with data frames...
test <- iris

# Simply modify in-place
test$Sepal.Length[test$Species == 'setosa' & test$Petal.Length < 1.5] %+=% 1

# Which is much nicer than typing:
test$Sepal.Length[test$Species == 'setosa' & test$Petal.Length < 1.5] <-
test$Sepal.Length[test$Species == 'setosa' & test$Petal.Length < 1.5] + 1
# ...which is over the 100 character limit for R doccumentation!

# %+=% and %-=% also work with strings

   x <- "ab"

   x %+=% "c"

   x %-=% "b"

   x == "ac" # TRUE

# %-=% can also take regular expressions

   x <- "foobar"

   x %-=% "[f|b]"

   print(x)
   # "ooar"

roperators documentation built on July 26, 2023, 5:27 p.m.