assign_ops: Assignment operators

Description Usage Arguments Examples

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

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

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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 May 1, 2019, 6:27 p.m.