CSC121 - R Console Friday January 26th --------------------------- > # Indexing vectors with values > v <- c(1:10) > v [1] 1 2 3 4 5 6 7 8 9 10 > v[2:7] [1] 2 3 4 5 6 7 > # Index vectors with an expression > # we have to be careful > v[3:14 / 2] [1] 1 2 2 3 3 4 4 5 5 6 6 7 > # this is unexpected behaviour! > # We wanted to index from 3 to 7 > # To properly index with an expression, surround the expression with round brackets > v[3:(14 / 2)] [1] 3 4 5 6 7 > > # Empty Vectors > # We've been dealing with vectors that have values > # In R however, we can have vectors that have no values in them. > # These are called empty vectors > # To create an empty vector, you have to specify the type of data that the vector should have (remember that vectors can only hold one type of data) > # To make an empty numeric vector: > numeric(0) numeric(0) > # 'numeric(0)' represents an empty vector in R > emptyVector <- numeric(0) > emptyVector numeric(0) > length(emptyVector) [1] 0 > emptyVector[1] [1] NA > # NA means that the data is Not Available > # there is no data there to find > emptyVector[1] <- 3 > emptyVector [1] 3 > # By assigning a value at index 1, emptyVector is no longer empty > emptyVector[5] <- 3 > emptyVector [1] 3 NA NA NA 3 > # Be careful when you assign values in a vector > # If you assign a number to a vector at some index, you should ensure that the indexes before it are also assigned > # Having NA in your vector is usually a sign that you did something *wrong* > # If you want to indicate something like not having a value at some index, > # Then you should fill those NAs with zeros > newVector <- numeric(7) > newVector [1] 0 0 0 0 0 0 0 > # This can be useful when you want to start with a 'blank' vector, but not have any NA's in it > # You can set values to every index without having to assign anything in between > newVector[5] <- 87 > newVector [1] 0 0 0 0 87 0 0 > # This will be useful later when we work with real data > > # Removing one element from a one-element vector gives back an empty vector > v <- 3 > v[-1] numeric(0) > # this may be useful in E2. You may end up with an empty vector > # As a reminder: 0 is an even number. > > ## Logical Operators > > # We've learned about comparison operators: >, >=, <, <=, ==, != > # There are also some logical operators that can combine logical values to create new logical values > t1 <- TRUE > t2 <- TRUE > > # One of these operators is known as the 'AND' operator. It is represented by '&&' > # Given two logical values, t1 and t2, 't1 && t2' returns TRUE if and only if t1 and t2 are both true > t1 && t2 [1] TRUE > t2 <- FALSE > t1 && t2 [1] FALSE > > # Another operator is known as the 'OR' operator. It is represented by '||' > # Given two logical values, t1 and t2, 't1 || t2' returns TRUE if and only if *one* of t1 or t2 is true > t1 [1] TRUE > t2 [1] FALSE > t1 || t2 [1] TRUE > t2 <- FALSE > t1 <- FALSE > t1 || t2 [1] FALSE > > # We also have a NOT operator, represented by '!' > # Given one logical value t (unary operator), !t returns TRUE if t is FALSE, and return FALSE if t is TRUE > t1 [1] FALSE > !t1 [1] TRUE > t2 <- TRUE > !t2 [1] FALSE > > # We can use these logical operators to create conditions for if-statements by combinding different logical values > # if (t1 && t2) { > # ... > # } > > # Using logical values can makes your code cleaner and easier to read > # But be careful! Do not use logical operators on logical values that don't have anything to do with each other > # Your code might still work, but it will create messy code with a lot of extra if's andd else if's > # For example our LaptopPrice function from last time. > # It is cleaner to write two seperate if statements than to use && operator and then have to write more conditions for every other case anyway >