CSC121 - R Console Monday January 22th --------------------------- > # More Vectors and Vector manipulation > > # Indexing > # We saw last time that we can index vectors using a single index or a range > v <- c(4, 532, 7, 53, 92) > v[2] [1] 532 > v[3:5] [1] 7 53 92 > # Every time we index a vector, we get a new vector > # But, indexing like this does not give us the ability to create ANY new vector from the original > > # For example, what if I want all the odd numbered indexes? > # If we want to pick out a general range of values from a vector, we can index the vector *using* a vector! > v [1] 4 532 7 53 92 > oddIndexes <- c(1, 3, 5) > v[oddIndexes] [1] 4 7 92 > evenIndexes <- c(2, 4) > v[evenIndexes] [1] 532 53 > firstAndLastIndexes <- c(1, length(v)) > v[firstAndLastIndexes] [1] 4 92 > # Can index by directly writing a vector in the index > v[c(1,3,5)] [1] 4 7 92 > # But, using variable names for vector indexes can help a reader understand your logic, just like intermediate values > > # Combining Vectors # > # Since vectors are simply an ordered group of values, we can easily combine them > a <- c(1.3, 5.1, 3.3) > b <- c(200, 400, 100) > c(a, b) [1] 1.3 5.1 3.3 200.0 400.0 100.0 > # R tries to keep consistent formatting when printing out the values - notice the one decimal place after each value > # You can combine vectors any way you want > c(c(1,2), b) [1] 1 2 200 400 100 > c(c(1,2), c(b, c(a,2))) [1] 1.0 2.0 200.0 400.0 100.0 1.3 5.1 3.3 2.0 > # Make sure that you close every bracket you opened! > > # Most common use of combining vectors, is adding an element to the vector > t <- c(3, 5, 7) > # example: add the value 9 to the end of t > t <- c(t, 9) > t [1] 3 5 7 9 > # Add the value 1 to the beginning of t: > t <- c(1, t) > t [1] 1 3 5 7 9 > # What about adding an element somewhere in the middle of t? > # Example: insert the value 6 as the 4th element of t: > t <- c(t[1:3], 6, t[4:length(t)]) > t [1] 1 3 5 6 7 9 > t[4] [1] 6 > # notice that we have to assign the value of the new vector to t, which we created using the old value of t > # However, there are ways to change vectors in place > # Changing a value at a specific index: > t[1] <- 34 > t [1] 34 3 5 6 7 9 > # we can change multiple values > t[1:3] <- c(7,8,9) > t [1] 7 8 9 6 7 9 > # This is still an assignment statement. The difference is that you are now only changing part of the vector that the variable refers to > > x <- c(4, 6, 23) > y <- x > x[2] <- 7.7 > x [1] 4.0 7.7 23.0 > y [1] 4 6 23 > # Changing the element in one vector does not change it in any other vector > # y is still the same as when it was first assigned > > # Vector Arithmetic # > # Since these numeric vectors contain a bunch of numbers, we can naturally perform some mathematical operations one them > q <- c(1, 2, 3, 4, 5) > # Multiply every value in q by 2: > 2 * q [1] 2 4 6 8 10 > # This operation did NOT change the vector in place > q [1] 1 2 3 4 5 > # q retains its original value > # Add 3 to each element: > q + 3 [1] 4 5 6 7 8 > # We can use *any* of our mathematical operators > # Negation: > -q [1] -1 -2 -3 -4 -5 > q^2 [1] 1 4 9 16 25 > > # We can also do operations with both operands being *vectors* of more than length 1 > a <- c(1, 2) > b <- c(3, 4) > a + b [1] 4 6 > # For every index i, t[i] = a[i] + b[i] > # If the length of the vectors is DIFFERENT, then the shorter one is repeated in the same order to get to the length of the longer one > a < c(a, 3, 4) [1] FALSE FALSE TRUE TRUE > a [1] 1 2 > a <- c(a, 3, 4) > a [1] 1 2 3 4 > b [1] 3 4 > a + b [1] 4 6 6 8 > > # What happened? For indexes 1 and 2, it was the same as the case before > # when we got to index 3 of a, we ran out of values in b > # So it started repeating the value by going back to the beginning of b > # so we got: > # t[1] = a[1] + b[1], t[2] = a[2] + b[2] > # t[3] = a[3] + b[1], t[4] = a[4] + b[2] > > # Changing the types of vectors: > # Last time we saw that the type of the vector is the type of its elements: > a [1] 1 2 3 4 > typeof(a) [1] "double" > b <- c(as.integer(3), as.integer(4)) > typeof(b) [1] "integer" > b <- c(3, as.integer(4)) > typeof(b) [1] "double" > # We can convert the entire vector to integer: > as.integer(b) [1] 3 4 > b <- as.integer(b) > typeof(b) [1] "integer" > > # Creating some vectors quickly > # Let's say we want a whole sequence of numbers in order > # Example: 1 to 10 > c(1:10) [1] 1 2 3 4 5 6 7 8 9 10 > # Use a colon in c(...) to get a sequence of numbers > # Can combine sequences > c(1:5, 7:12) [1] 1 2 3 4 5 7 8 9 10 11 12 > # Since you can operations on them, we can get a bunch of even numbers > 2 * c(1:10) [1] 2 4 6 8 10 12 14 16 18 20 > # Multiples of 7: > 7 * c(1:10) [1] 7 14 21 28 35 42 49 56 63 70 > # Odd numbers: > 2 * c(1:10) - 1 [1] 1 3 5 7 9 11 13 15 17 19 > # Given a vector v <- c(1:10), how do we *pick out* the even numbers? > v <- c(1:10) > v[c(2, 4, 6, 8, 10)] [1] 2 4 6 8 10 > # not very efficient...we wrote out the vector we wanted in the index.. > # We're going to learn how to do this better. > > > ## LOGICAL VALUES ## > > # So far we've been looking at numeric values - this is the only type of data in R we've seen > # Let's look at a new data type > # This data type stems directly from the computer's ability to understand two numbers: 0 and 1 > # while numeric data can take on many, many values (lots of numbers out there), logical values can only take on two different values > # What are they? > # TRUE or FALSE > # These words are reserved in R for logical values - they are not variable names and you cannot assign anything to them > TRUE [1] TRUE > FALSE [1] FALSE > # Logical values are designed to represent the *truth values* of statements in R > # Here's one way to get a truth value: Comparison Operators > 5 > 4 [1] TRUE > 12 < 3 [1] FALSE > a <- 12 > a < 10 [1] FALSE > # the above are called 'more than' and 'less than' operators > a == 12 [1] TRUE > a == 9 [1] FALSE > # ^ equality operator > # We can also check if it's not equal to a value: > a != 9 [1] TRUE > # != means 'not equal to' > # we can do 'greater than or equal to': > a >= 12 [1] TRUE > a <= 11 [1] FALSE > # <= means 'less than or equal to'. <- is the assignment operator > # Comparisons of expressions: > 1 + 2 > 5 [1] FALSE > # 1 + 2 > 5 evaluates to 3 > 5, which evaluates to FALSE > 6 < 2 * 5 [1] TRUE > 2 + 2 == 4 [1] TRUE > # As long as there is a value on both sides of the comparison operator, you will get back a logical value > > # Notice the [1] on the output of all of these logical values. What does this mean? > # Logical values, like numbers, also go into vectors > v <- c(TRUE, FALSE, TRUE) > v [1] TRUE FALSE TRUE > # You can also create a vector of logical values using the logical(x) function, where x is the number values you want. They will all be set to FALSE initially > logical(5) [1] FALSE FALSE FALSE FALSE FALSE > > # Converting between numeric and logical values: > as.double(TRUE) [1] 1 > as.double(FALSE) [1] 0 > # Can we convert the other way? > # Yes - use 'as.logical(x)' > as.logical(1) [1] TRUE > as.logical(0) [1] FALSE > # What about other numbers? > as.logical(4) [1] TRUE > as.logical(27) [1] TRUE > as.logical(-12) [1] TRUE > # All numbers except 0 are converted to TRUE >