# Owen-calculator.R # # This script works through the Owen calculator chapter with some extra # discussion. # # modified from: Owen, W. J., 2007. The R Guide, version 2.3. # # created 9-7-10 by M. Dillon # # last modified: 29 Aug 2011 # ##################################################################### 2+3 3/2 2^3 # beware of order of operations here 4^2-3*2 (56-14)/6 - 4*7*10/(5^2-5) sqrt(2) abs(2-4) cos(4*pi) log(0) factorial(6) choose(52,5) # I don't understand how this works, so I look at the help file: ?choose # Vector arithmetic # Here we use the "concatenate" function c() to create "vectors", which # are lists of numbers. We assign them to "variables" using the # assignment operator "<-" (a less than sign followed by a dash). # The quick way to type this is _ (underscore). # x <- c(1, 2, 3, 4) # note spaces after commas for legibility/readability y <- c(5, 6, 7, 8) x*y # How did it perform this calculation? # The simple * does "element-wise" multiplication-- # each element in one vector is multiplied by the # corresponding element in the other. What happens if # vectors aren't the same length? z <- c(1, 2, 3) x*z # This element-wise effect applies for all the other # simple mathematical operators as well y/x y-x x^y cos(x*pi) + cos(y*pi) # Note that pi is a special variable that is already # assigned a value by R: pi # Also notice the distinction here between a scalar # (like pi), which is simply a number, and a vector # which is a list of numbers. Of course, a scalar # can operate on a vector of any length--it # operates on every element of the vector: pi*x pi*z pi*pi s <- c(1, 1, 3, 4, 7, 11) length(s) sum(s) prod(s) cumsum(s) diff(s) diff(s, lag = 2) # what is this lag thing? ?diff # try the examples for diff listed in the help file. # Matrices # # Like scalars and vectors, a matrix (plural matrices) # is another way to store data. As you'll find out, # it is a very powerful way to store data, particularly # if you are a "mathy" type and know how to manipulate # them (I am not and don't). a <- c(1,2,3,4,5,6,7,8,9,10) A <- matrix(a, nrow = 5, ncol = 2) # Note that variable names are case sensitive: # a does not equal A! A # Why did it fill down the columns first? ?matrix # If you look at the help file, you'll see that one # of the arguments to the function "byrow" has a # preset value of "FALSE". Many functions will # have arguments with preset values: if you don't tell # it differently, it will use this preset value. # # We can also specify the arguments to change the way the # function works: B <- matrix(a, nrow = 5, ncol = 2, byrow=TRUE) B # Now it filled the matrix "by row" # Also notice that we don't have to specify "data=a", which # you might think was necessary after reading the help file. # R assumes that the first argument we send is "data". # #!!!! These preset arguments are a really important reason # to read the help file for a function when you first # start using it. Not only does it enlighten you as # to all the options, but it also makes you aware of # "hidden" switches that are controlling the behavior # of the function. C <- matrix(a, nrow = 2, ncol = 5, byrow = TRUE) C # More matrix manipulations t(C) B%*%C # This is very different from *! # matrix multiplication instead of element-wise multiplication # Matrix dimensions matter: B%*%B # You can assign calculations to new variables: D <- C%*%B D det(D) solve(D)