################################################################################ ### R BASICS WORKSHOP ### ### PRESENTATION 5: OPERATORS ### ### ### ### Center for Conservation and Sustainable Development ### ### Missouri Botanical Garden ### ### Website: rbasicsworkshop.weebly.com ### ################################################################################ ### A. TYPES AND HELP WITH OPERATORS ##################################################### # Main types of operators: # 1. Arithmetic # 2. Comparison # 3. Logical # 4. Other # Help: ?* # This does not work help ("*") # This works ### B. ARITHMETIC OPERATORS #################################################### # A pair of vectors and matrices that we will use for examples: p <- seq(from = 1, to = 10, b = 1) z <- seq(9, 18, 1) p z P <- matrix(seq(1, 25, 1), ncol = 5) Z <- matrix(seq(26, 50, 1), ncol = 5) P Z # Arithmetic operations are the most common: # + Sum # - Subtraction # * Multiplication # / Division # ^ Power # %% Module - calculates the remainder in a division # %/% Integer division - calculates the integer portion of a division ## IMPORTANT: It is critical to understand how operators work on different ## situations. This also applies to other types of operators! ## Case 1. Two scalars (single numbers): the simplest case ## 10 + 3 10 - 3 10 * 3 10 / 3 10 ^ 3 10 %% 3 10 %/% 3 ## Case 2. A vector or matrix and a scalar ## p P p - 3 P - 3 # The operation occurs between each element in the matrix and the scalar. The # result is the same size as the vector or matrix. p ^ 3 P ^ 3 p %% 3 P %% 3 ## Case 3. Two vectors or matrices of the same size ## z Z p * z P / Z # In this case, the operation is between the two corresponding elements # of each vector or matrix. The result is the same size as the original objects. ## Case 4. Two vectors or matrices of different sizes ## length(p) p - c(1,2) p - rep(c(1,2), time = 5) p - c(1,2,3) ## IMPORTANT: in the first case, the length of the small object (2) is a ## multiple of the length of the large (10). The operation is done "recycling" ## or repeating elements of the smaller object. In the second case, the length ## of the smaller object (3) is not a multiple of the ## large one (10). This generates a warning message. ## Case 5. Operations with NA # In any situation where there are NAs, operations return NA p - NA p - c(1, NA) ### B. COMPARATIVE OPERATORS ################################################### # > Greater than # < Less than # >= Greater than or equal to # <= Less than or equal to # == equal than (not a single "=", but both together!) # != Not equal to ## IMPORTANT: Comparisons between objects follow the same rues as those for ## arithmetic operators. ## These operators return logical values: TRUE or FALSE p < 5 z > 8 p <= z Z >= P p == c(33, 37) Z != c(NA, 37) # Although not operators, functions *is.na* and *!is.na* are useful in this context x <- c(3, NA, 34, 23, 19, NA) is.na(x) !is.na(x) p < c(5, NA) is.na(p < c(5, NA)) !is.na(p < c(5, NA)) ### C. LOGICAL OPERATORS ####################################################### # &: Means 'and' # |: Means 'or' 5 > 3 # This is TRUE 5 < 3 # This is FALSE 2 > 4 # This is FALSE 2 < 4 # This is TRUE ## & ## # is 5 less than three AND 2 greater than 4? 5<3 & 2>4 # This is FALSE because NONE of comparisons are TRUE # is 5 more than 3 AND 2 greater than 4? 5>3 & 2>4 # This is also FALSE because NOT ALL comparisons are TRUE # is 5 more than 3 AND 2 less than 4? 5> 3 & 2<4 # Now both comparisons are TRUE, so all together is also TRUE ## | ## # is 5 less than 3 OR 2 greater than 4? 5<3 | 2>4 # This is FALSE because NONE of comparisons are TRUE # is 5 greater than 3 OR 2 greater than 4? 5>3 | 2>4 # This is TRUE because AT LEAST ONE comparisons is TRUE # is 5 greater than 3 OR 2 less than 4? 5>3 | 2<4 # This is also TRUE because AT LEAST ONE comparisons are TRUE ## IMPORTANT: When *&* or *|* used with vectors or matrices, comparisons are ## made element by element. For example: p > 5 z < 18 p>5 & z<18 # is TRUE only for items where p>5 AND z<18 are TRUE p>5 | z<18 # is TRUE for items where p>5 OR z<18 are TRUE ## IMPORTANT: *&&* AND *||* also exist but make the comparison only for the ## first element of vectors p> 5 && z <18 p> 5 || z <18