JS.Operator.Assignment: 'Javascript'-style Binary Operator Assignment

Description Usage Arguments Value See Also Examples

Description

If you are familiar with Javascript, you may miss +=, -=, *=, /= very much. The operator can be +, -, *, /, ^, **, \, mod, root.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
lhs %+=% rhs

lhs %-=% rhs

lhs %*=% rhs

lhs %/=% rhs

lhs %^=% rhs

lhs %**=% rhs

lhs %\=% rhs

lhs %mod=% rhs

lhs %root=% rhs

Arguments

lhs

Left hand side argument.

rhs

Right hand side argument.

envir

Environment for operation to take place. Default parent.frame()

Value

Nothing, but lhs is already modified.

See Also

You can also use the compound operator %<>% and the operator aliases provided in magrittr to realize simiar effects.

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
## Not run: 
## update an object entirely
a <- 3
a %+=% 2     # 3 --> (3 + 2) --> 5, equivalent to a %<>% add(2)
a %-=% 2     # 5 --> (5 - 2) --> 3, equivalent to a %<>% subtract(2)
a %*=% 2     # 3 --> (3 * 2) --> 6, equivalent to a %<>% multiply_by(2)
a %/=% 2     # 6 --> (6 / 2) --> 3, equivalent to a %<>% divide_by(2)
a %^=% 2     # 3 --> (3 ^ 2) --> 9, equivalent to a %<>% raise_to_power(2)
a %**=% 2    # 9 --> (3 ** 2) --> 81, same as above
a %\=% 30    # 81 --> (81 %% 30) --> 21, equivalent to a %<>% mod(30)
a %mod=% 8   # 21 --> (21 %% 8) --> 5, equivalent to a %<>% mod(8)
a %root=% 2  # 5 --> (5 ^ (1/2)) --> 2.236, equivalent to a %<>% sqrt

## object and object
a <- 1:4
b <- 4:1
a %+=% b     # a --> c(5,5,5,5)
a %*=% b     # a --> c(20, 15, 10, 5)  (c(5,5,5,5) * c(4,3,2,1))

## update an object partially
b <- 1:4
b[1] %+=% 1     # b --> c(2,2,3,4)

c <- list(list(A=1, B=2:3), list(C=4))
c[[1]]$A %-=% 1 # c[[1]]$A --> 0, rest elements are not changed

1 %+=% b        # simply print 3 (1 + b[2]), but no variable is changed
1 %+=% 1        # simply print 2 (1 + b[1]), but no variable is changed

## End(Not run)

madlogos/aseskit documentation built on June 26, 2019, 12:17 a.m.