The Big R-Book. Philippe J. S. De Brouwer
arithmetic – operators
Arithmetic operators act on each element of an object individually.
operator – arithmetic
v1 <- c(2,4,6,8) v2 <- c(1,2,3,5) v1 + v2 # addition ## [1] 3 6 9 13 v1 - v2 # subtraction ## [1] 1 2 3 3 v1 * v2 # multiplication ## [1] 2 8 18 40 v1 / v2 # division ## [1] 2.0 2.0 2.0 1.6 v1 %% v2 # remainder of division ## [1] 0 0 0 3 v1 %/% v2 # round(v1/v2 -0.5) ## [1] 2 2 2 1 v1 ∧ v2 # v1 to the power of v2 ## [1] 2 16 216 32768
addition
substraction
multiplication
division
power
While the result of the sum will not surprise anyone, the result of the multiplicationmight come as a surprise for users of matrix oriented software such as Mathlab or Octave for example. In R an operations is always element per element – unless explicitly requested. For example, the dot-product can be obtained as follows.
v1 %*% v2 ## [,1] ## [1,] ss 68
4.4.2 Relational Operators
Relational Operators compare vectors element by element
relational operators
operator – relational
v1 <- c(8,6,3,2) v2 <- c(1,2,3,5) v1 > v2 # bigger than ## [1] TRUE TRUE FALSE FALSE v1 < v2 # smaller than ## [1] FALSE FALSE FALSE TRUE v1 <= v2 # smaller or equal ## [1] FALSE FALSE TRUE TRUE v1 >= v2 # bigger or equal ## [1] TRUE TRUE TRUE FALSE v1 == v2 # equal ## [1] FALSE FALSE TRUE FALSE v1 != v2 # not equal ## [1] TRUE TRUE FALSE TRUE
bigger than
smaller than
bigger or equal
equal
not equal
4.4.3 Logical Operators
Logical Operators combine vectors element by element. While logical operators can be applied directly on composite types, theymust be able to act on numeric, logical or complex types in order to produce understandable results.
operator – logical
v1 <- c(TRUE, TRUE, FALSE, FALSE) v2 <- c(TRUE, FALSE, FALSE, TRUE) v1 & v2 # and ## [1] TRUE FALSE FALSE FALSE v1 | v2 # or ## [1] TRUE TRUE FALSE TRUE !v1 # not ## [1] FALSE FALSE TRUE TRUE v1 && v2 # and applied to the first element ## [1] TRUE v1 || v2 # or applied to the first element ## [1] TRUE v1 <- c(TRUE,FALSE,TRUE,FALSE,8,6+3i,-2,, NA) class(v1) # v1 is a vector or complex numbers ## [1] “complex” v2 <- c(TRUE) as.logical(v1) # coerce to logical (only 0 is FALSE) ## [1] TRUE FALSE TRUE FALSE TRUE TRUE TRUE FALSE NA v1 & v2 ## [1] TRUE FALSE TRUE FALSE TRUE TRUE TRUE FALSE NA v1 | v2 ## [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
Note that numbers different from zero are considered as TRUE, but only zero is considered as FALSE. Further, NA is implemented in a smartway. For example, in order to assess TRUE & NA
we need to know what the second element is, hence it will yield NA. However, TRUE | NA
will be true regardless what the second element is, hence R will show the result.
FALSE | NA ## [1] NA TRUE | NA ## [1] TRUE FALSE & NA ## [1] FALSE TRUE & NA ## [1] NA FALSE | NA | TRUE | TRUE ## [1] TRUE TRUE & NA & FALSE ## [1] FALSE
4.4.4 Assignment Operators
R has multiple ways to express an assignment. While it is possible to mix and match, we prefer to choose just one and stick with it. We will use <-
.
operator – assignment
and
or
# left assignment x <- 3 x = 3 x<<- 3 # right assignment 3 -> x 3 ->> x #chained assignment x <- y <- 4
not
The <<-
or ->>
operators change a variable in the actual environment and the environment above the actual one. Environments will be discussed in Section 5.1 “Environments in R” on page 110. Till now we have always been working in the command prompt. This is the root-environment. A function will create a new (subordinated) environment and might for example use a new variable. When the function stops running that environment stops to exist and the variable exists no longer.3
assignment – left
assignment – right
assignment – chained
Generally it is best to keep it simple, most people will expect to see a left assignment, and while it might make code a little shorter, the right assignment will be a little confusing for most readers. Best is to stick to =
or <-
and of course <<-
whenever assignment in a higher environment is also intended.
In some special cases, such as the definition of parameters of a function, it is not possible to use the “arrow” and onemust revert to the =
sign. This makes sense, because that is not the same as a traditional assignment.
mean(v1, na.rm = TRUE) # works (v1 is defined in previous section) ## [1] 1.75+0.375i mean(v1, na.rm <- TRUE) # fails ## Error in mean.default(v1, na.rm <- TRUE): ‘trim’ must be numeric of length one