CSC121 - R Console Friday January 12th --------------------------- > # Last time we saw that as.integer(x) is a function > as.integer(1 + 2 + 0.6) [1] 3 > # Let's go through the evaluation stages > # The function: as.integer(x) > # Argument to the function in general: x > # In our specific case of as.integer(1 + 2 + 0.6), the argument is: 1 + 2 + 0.6 > # ^ the argument is not '3.6' because we haven't evaluated it yet > # Now, our function call steps: > # Step 1: Evaluate the argument to the function > # 1 + 2 + 0.6 = 3.6 > # The value of the argument to the function is 3.6 > # Step 2: Call the function as.integer(x) with the value 3.6 > # After we called the function, we get a return value > # The return value of as.integer(3.6) = 3 > as.integer(1 + 2 + 0.6) [1] 3 > > # Arguments that are functions themselves > t <- sqrt(abs(-4)) > # Step 1: Evaluate the argument to the function sqrt > # The argument is abs(-4) the argument is a function call! > # So we have to evaluate abs(-4) > # The argument to abs(-4) is -4 > # After calling abs(-4) we get 4 > # The return value of abs(-4) is 4 > # We've evaluated the argument to sqrt(x), in this case it is 4 > # Step 2: Call the function with the value 4 > # The return value of sqrt(4) = 2 > t [1] 2 > > # some other math functions: > # Sinusoidal functions: sin(x) and cos(x) > # Pi = 3.14159...... can't store infinite digits. We'll talk later about how R handles these > pi [1] 3.141593 > sin(pi / 2) [1] 1 > cos(pi) [1] -1 > P <- abs(cos(pi)) > # what do we expect? > # The argument to abs(x) is cos(pi) > # the value of the argument is cos(pi) = -1 > # So now we have abs(-1) > # So P should have the value 1 > P [1] 1 > # You want to get faster at understanding how function calls and their arguments get evaluated > # Practise!!! > > # Important and USEFUL function!!!!!!! > # help(x) > # Put as an argument to help(x) a name of a function > help(sqrt) > # help(x) return on the right side of RStudio some help documentation for that function > help(as.integer) > >