CSC121 - R Console Wednesday January 24th --------------------------- > # Last time, I wanted to mention, that we can get a new vector by removing an element from an existing vector > # Example: remove the element at index 3 > v <- c(1,2,3,4,5) > # we use negative indexes > v[-3] [1] 1 2 4 5 > v [1] 1 2 3 4 5 > v <- v[-3] > v [1] 1 2 4 5 > v[c(-2, -3)] [1] 1 5 > v > # remember that we can only change elements of vectors in place like this: [1] 1 2 4 5 > v[1] <- 4 > v [1] 4 2 4 5 > > # Back to logical values > # Last time we introduced TRUE and FALSE as logical values > # These values are used to evaluate the truth values of statements > 12 > 5 [1] TRUE > 1 + 2 == 5 [1] FALSE > # Let's now combine numerical vectors with logical values > > # Consider the vector v: > v <- c(1:5) > v [1] 1 2 3 4 5 > # We saw that the logical comparisons like 4 > 3 give us a logical value (TRUE) > # What if we do a comparison on the entire vector? > v > 3 [1] FALSE FALSE FALSE TRUE TRUE > # We get a vector of logical values that gives us a comparison on every element of v, and puts it in the corresponding index > # What will these give: > v <= 4 [1] TRUE TRUE TRUE TRUE FALSE > v == 2 [1] FALSE TRUE FALSE FALSE FALSE > v != 1 [1] FALSE TRUE TRUE TRUE TRUE > > # Indexing vectors using logical values > # We can create a slice of a vector by using logial vectors as the index! > v [1] 1 2 3 4 5 > v[c(TRUE, FALSE, FALSE, TRUE, FALSE)] [1] 1 4 > # It picked out the indexes that are TRUE in the logical vector (index 1 and 4) > # Since we can do a comparison operation ot get a logical vector, we can index v based on some condition > v > 3 [1] FALSE FALSE FALSE TRUE TRUE > v[v > 3] [1] 4 5 > v[v != 2] [1] 1 3 4 5 > # Let's revisit our question from last time: > # Given v <- c(1:10), how do we pick out the even numbers of v? > v <- c(1:10) > v [1] 1 2 3 4 5 6 7 8 9 10 > 2 %% 2 [1] 0 > 1 %% 2 [1] 1 > v[v %% 2 == 0] [1] 2 4 6 8 10 > v[v %% 2 == 1] [1] 1 3 5 7 9 > # If the logical vector we're indexing at is smaller than the length of the vector we're indexing, it will just repeat the logical index vector > v [1] 1 2 3 4 5 6 7 8 9 10 > v[c(TRUE, FALSE, TRUE)] [1] 1 3 4 6 7 9 10 > p <- c(1,4,7,10) > q <- c(44,5,3,26) > c(q[q < 7], p[p >= 10]) [1] 5 3 10 > > # There are some built-in R functions that return logical values > # is 4.3 a double? > is.double(4.3) [1] TRUE > is.double(4) [1] TRUE > is.double(as.integer(4)) [1] FALSE > is.integer(5.4) [1] FALSE > is.logical(TRUE) [1] TRUE > is.logical(24234) [1] FALSE > # Often you will name logical variables with words like 'is', or 'has' > isRaining <- FALSE > # if someone has a bike: > hasBike <- TRUE > > # Running our if-statement functions > source('~/Desktop/W3/jan24_lecture/if_statement_functions.R') > TicketPrice(3) [1] 4.25 > TicketPrice(34) [1] 7.5 > TicketPrice(109) [1] 4.75 > source('~/Desktop/W3/jan24_lecture/if_statement_functions.R') > FlightPrice(500, FALSE) [1] 500 > FlightPrice(500, TRUE) [1] 900 > source('~/Desktop/W3/jan24_lecture/if_statement_functions.R') > NumberFun(2,2) [1] 1 > NumberFun(2,1) [1] 2 > NumberFun(3,1) [1] 3 > NumberFun(3,5) [1] 3 > NumberFun(3,6) [1] 3 > source('~/Desktop/W3/jan24_lecture/if_statement_functions.R') > LaptopPrice(FALSE, FALSE) [1] 1299 > LaptopPrice(TRUE, FALSE) [1] 899 > LaptopPrice(FALSE, TRUE) [1] 1799 > LaptopPrice(TRUE, TRUE) [1] 1399