# Arithmetic and logical expressions #0. Precedence #See Table 3.1 on p. 50of V&R, but some common situtations, highest to lowest: # function(..) # ^ #exponentiation # * / #multiplication, division# # + - #addition, subtraction # > < <= == != #comparison operators #but can always use parentheses to force a precedence x <- 36 y <- 7 sqrt(x)*7-2 < x+y^2 (((sqrt(x))*7)-2) < (x+(y^2)) #identical to above but "clear" precedence #1. element by element operations on two vectors x <- 1:10 y <- 11:20 x+y x-y x*y x/y x^y #2. recycling: when operating on 2 vectors with unequal length # length of output = length of longer vector, recycling the # shorter vector's values till match longer vector's length x <- 2 y <- 1:6 x+y #2+1, 2+2, 2+3, 2+4, 2+5, 2+6 x <- c(2,9) x+y #2+1, 9+2, 2+3, 9+4, 2+5, 9+6 #3. Integer division and modulo reduction 7 %/% 3 #answer 2, number of times 3 goes into 7 7 %% 3 #answer 1, 7 - (7 %/% 3)*3 #4 Logical expressions and functions with logical arguments x <- c(1,3,5) y <- c(2,2,4) x < y x <= y x != y any(x