The Big R-Book. Philippe J. S. De Brouwer

Чтение книги онлайн.

Читать онлайн книгу The Big R-Book - Philippe J. S. De Brouwer страница 35

The Big R-Book - Philippe J. S. De Brouwer

Скачать книгу

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

       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

      image Note – Numeric equivalent and logical evalutation

      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

       assignment – left

       assignment – right

       assignment – chained

      image Hint – Assignment

      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

      While the <<- seems to do exactly the same, it changes also the value of the variable in the environment above the actual one. The following example makes clear how <- only changes the value of x while the function is active, but <<- also changes the value of the variable x in the environment where the function was called from.

      # f # Assigns in the current and superior environment 10 to x, # then prints it, then makes it 0 only in the function environment # and prints it again. # arguments: # x -- numeric f <- function(x) {x <<- 10; print(x); x <- ; print(x)} x <- 3 x ## [1] 3 # Run the function f(): f(x) ## [1] 10 ## [1] 0 # Only the value assigned with <<- is available now: x ## [1] 10

      Digression – For C++ programmers

      If you are moving from C++, you will miss the speed and functionality of passing variable by their pointer. The <<- operator will provide you the ability to change a variable in the environment above the function.

      image Warning – Sparingly change variables in other environments

      4.4.5

Скачать книгу