CSC121 - R Console Wednesday January 10, 2018 --------------------------- > # Let's work with assignment statements > x <- 4 > x [1] 4 > y <- 5 > y [1] 5 > # When you refer to a variable (type it and press enter) > # you always get the last value stored in it > x <- 6 > x [1] 6 > x <- x + 4 > # Step 1: Evaluate expression on the right hand side > # x + 4 = 6 + 4 = 10 > # The value on the right hand side is 10 > # Step 2: Assign the value that we evaluated on the right hand side > # to the variable name on the left hand side > x [1] 10 > y [1] 5 > y <- x + y > # Step 1: Evaluate right hand side > # x + y = 10 + 5 = 15 > # Step 2: Assign the value 15 to the variable y > y [1] 15 > # If we keep just reffering to y, R will always give back the same value > y [1] 15 > y [1] 15 > y [1] 15 > y <- 17 > y [1] 17 > typeof(y) [1] "double" > typeof(x) [1] "double" > p <- 3 > # Non-integers: > q <- 2.3 > t <- p + q > t [1] 5.3 > t <- p + as.integer(q) > t [1] 5 > # Let's see what happens when we change data types > a <- 4.25 > a [1] 4.25 > typeof(a) [1] "double" > as.integer(a) [1] 4 > a [1] 4.25 > # Still 4.25! > # as.integer(a) does NOT change the value of a > # The *only* way to change the value of a > # is to use an assignment statement to assign a new value to the variable a > a <- as.integer(a) > a [1] 4 > b <- 5 > c <- a + b > c [1] 9 > a <- 2 > a [1] 2 > # Question: what is the value of c? > c [1] 9 > c <- a + b > c [1] 7 > # When we assigned c to be 9, we can't change its value until > # we assign a new value to it > # So even though we changed a later > # c's value wasn't assigned anything new > # Remember: You can *only* change the value of a variable with an assignment statement. > > > ### FUNCTIONS ### > > # In math, we can think of a function as a machine that: > # 1. takes some input > # 2. does something with that input > # 3. produces some output > > # For example: f(x) = 3x > # It takes some input: x > # Does something with that input: multiplies x by 3 > # produces some output: gives you back 3x > > # Part of the structure of most proramming languages is the ability to use this kind of 'function' system > # As we did with variables, let's see what can find out about functions in the console > > # what looks like a function? > # as.integer(x) > # - just replace f in "f(x)" with as.integer > > # how do we know as.integer is a function? > # by our definition: > # 1. takes some input: x (some number) > # 2. does something with that input: finds the next lowest integer (or the same number if it is already an integer) > # 3. produces some output: gives you back that lowest integer in the R console > > # So, as.integer(x) behaves like a function > # we can give it different inputs: > as.integer(4.5) [1] 4 > as.integer(6.352354545) [1] 6 > as.integer(8) [1] 8 > > # Whenever we give a function in R a value and press Enter, > # we call this "calling the function" > # as.integer(2.3) means "calling as.integer(x) with the value 2.3" > # The value that is given back to us when the function is done being called > # is called the "Return value" > as.integer(2.3) [1] 2 > # as.integer(2.3) "returned" 2 > > # R has many built-in functions > # we already saw as.integer(x) and as.double(x) > # Another one is sqrt(x) > sqrt(9) [1] 3 > # sqrt(x) returns the square root of the value it is called with > sqrt(10 + 6) [1] 4 > # typeof is also a function > # typeof(x) returns the type of the value x > > # Reminder: as.double(x) returns the 'double' version of x > as.double(2.435) [1] 2.435 > # It gives back the same value because 2.435 is already a double