CSC121 - R Console Wendesday February 7th -------------------------- > # LISTS > > # Lists are another R data structure that can store a group of values (just ) > # (just like a vector can) > # However, unlike vectors, lists can store *multiple* data types at a time > n <- 121 > s <- "csc" > v <- c(432, 2, 95) > t <- TRUE > # We create a list using the list(..) function, which puts together a bunch of > # elements into a list > L <- list(n, s, v, t) > L [[1]] [1] 121 [[2]] [1] "csc" [[3]] [1] 432 2 95 [[4]] [1] TRUE > typeof(L) [1] "list" > > # A list is ordered, and in the console we can see the index of the elements > # We can index the list using double square brackets > L[[1]] [1] 121 > L[[2]] [1] "csc" > L[[3]] [1] 432 2 95 > # The third element is a vector, so we get back the entire vector, not just the first element of the vector > # What if we index with one square bracket instead of two? > L[2] [[1]] [1] "csc" > typeof(L[2]) [1] "list" > typeof(L[[2]]) [1] "character" > # We don't get back the value at index 2 if we use square brackets > # Instead, we get back another list, with only one element (the element we indexed) > # You *must* use double square brackets to get the actual value of the element of the list > > # Since we can create new lists using an index in single square brackets, > # we can use themto create any sublist of L > L[c(2, 4)] [[1]] [1] "csc" [[2]] [1] TRUE > # L[c(2,4)] returns a list with only the 2nd and 4th elements > # We can also index a list using logical values, the same way we did with vectors > L[c(TRUE, FALSE, FALSE, TRUE)] [[1]] [1] 121 [[2]] [1] TRUE > # We cannot, however, use logical expressions as indexes in the same way we did for vectors > L[L > 2] Error: (list) object cannot be coerced to type 'double' > L[[L > 2]] Error: (list) object cannot be coerced to type 'double' > L2 <- list(1, 2, 3) > L2[L2 > 1] [[1]] [1] 2 [[2]] [1] 3 > # If the list contains all numbers, than you can index using a logical expression involving numbers > # Otherwise, the list might not be able to convert all of the values to another type > # In some cases, you can convert the elements of the entire list to a specific type > as.character(L) [1] "121" "csc" "c(432, 2, 95)" "TRUE" > # But not in all cases > as.double(L) Error: (list) object cannot be coerced to type 'double' > # R especially does not like converting lists with vectors > L3 <- list(c(2,3,4)) > L3 [[1]] [1] 2 3 4 > as.double(L3) Error: (list) object cannot be coerced to type 'double' > > # As an aside, we only use capital 'L' when dealing with arbitrary lists > # Use the usual naming conventions for any other list name: studentList > > # In lab today: Lists of lists > K <- list(list(1, 2, "csc121"), list(3, 4, "csc207")) > K [[1]] [[1]][[1]] [1] 1 [[1]][[2]] [1] 2 [[1]][[3]] [1] "csc121" [[2]] [[2]][[1]] [1] 3 [[2]][[2]] [1] 4 [[2]][[3]] [1] "csc207" > # Notice that the output can be a bit confusing! > # Since this is a list of lists, it needs to differentiate between the outer list > # and the two inner lists. > # The [[1]] and [[2]] which are on their own lines indicate the indexes of > # the outer list (one index for each inner list) > # [[1]][[1]] means the first index of the first inner list > # [[1]][[2]] means the second index of the first inner list > K[[1]][[1]] [1] 1 > K[[1]][[2]] [1] 2 > # [[2]][[1]] means the first index of the second inner list > K[[2]][[1]] [1] 3 > K[[2]][[3]] [1] "csc207" > K <- list(list(1, 2, "csc121"), list(3, 4, "csc207")) > K[[2]] [[1]] [1] 3 [[2]] [1] 4 [[3]] [1] "csc207" > # K[[2]] is a list > > K[2] [[1]] [[1]][[1]] [1] 3 [[1]][[2]] [1] 4 [[1]][[3]] [1] "csc207" > # K[2] is a list that contains a list > # Be careful with the indexes and number of square brackets! You will practise > # in the lab today. > > source('~/Desktop/for_loop_list.R') 3 three 4 56 > source('~/Desktop/for_loop_list.R') Error in cat(e) : argument 1 (type 'list') cannot be handled by 'cat' > source('~/printing_to_console.R') [1] 4 > value [1] 4 > source('~/printing_to_console.R') [1] 4 > source('~/printing_to_console.R') 4 > source('~/printing_to_console.R') > ReturnStatement(4) [1] 4 > source('~/printing_to_console.R') > source('~/printing_to_console.R') [1] 4 4