R Console Wednesday, February 14 ----------------------- > # More list manipulation > > # Empty Lists > # Unlike empty vectors which have the type name followed by (0), empty lists are made with no arguments > emptyList <- list() > emptyList list() > list(0) [[1]] [1] 0 > # We get a non-empty list with a value - 0, when we use list(0) > > # Last time we saw the append() function > L <- list("string", 43, c(3, 4)) > L <- append(L, 57) > L [[1]] [1] "string" [[2]] [1] 43 [[3]] [1] 3 4 [[4]] [1] 57 > # What if we wanted to append a vector? > append(L, c("first", "second")) [[1]] [1] "string" [[2]] [1] 43 [[3]] [1] 3 4 [[4]] [1] 57 [[5]] [1] "first" [[6]] [1] "second" > # Notice that the elements simply get added at the end as separate list elements. > # We want *one* list element with the whole vector > # How do we fix this? Enclose the vector in a list > append(L, list(c("first", "second"))) [[1]] [1] "string" [[2]] [1] 43 [[3]] [1] 3 4 [[4]] [1] 57 [[5]] [1] "first" "second" > # Combining lists with the c(..) > # We can combine the values of two separate lists using the c(..) function in much the same way that we did with vectors > L1 <- list(2, c("hi", "hey")) > L2 <- list(3, c("bye", "later")) > c(L1, L2) [[1]] [1] 2 [[2]] [1] "hi" "hey" [[3]] [1] 3 [[4]] [1] "bye" "later" > # We get one list with the elements of both L1 and L2 in the same order > > # New logical operator: %in% > # Usage: x %in% y > # x %in% y returns TRUE iff x is an element of y > # y can be any set, like a list or a vector > v <- c(45, 32) > 45 %in% v [1] TRUE > 47 %in% v [1] FALSE > L3 <- list(21, "hello") > "hello" %in% L3 [1] TRUE > "hey" %in% L3 [1] FALSE > # Note: Only one-element vectors are supported. You can not properly check if a long vector is in a list > L4 <- list(c(1,2,3)) > c(1, 2, 3) %in% L4 [1] FALSE FALSE FALSE > # Does not give back the desired 'TRUE' output > # Only use for single elements > > # DICTIONARIES (in R, 'named lists') > # In computer science, a dictionary is a data structure that allows us to 'look up' values using *names* rather than index numbers > # We can name each element of a list and use it to look up that element in the list > # An example of naming elements in a list: > L5 <- list(a=4, b=5, c=6) > L5 $a [1] 4 $b [1] 5 $c [1] 6 > # We gave the names a, b, and c to the values inside L5 > # In dictionaries, these names are called 'keys' > # The elements that the keys refer to are called 'values' > # So we call these *key-value pairs*. Every key is paired with some value. > L5 <- list(a=4, b=5, c=6) > # key 'a' refers to the value 4 > # key 'b' refers to the value 5 > # key 'c' refers to the value 6 > # We can look up the value for each key in two ways: > # First way is to index with a string > L5[["a"]] [1] 4 > L5[["b"]] [1] 5 > L5[["c"]] [1] 6 > L5[["d"]] NULL > # Second way to index is to use the dollar sign $ notation > L5$a [1] 4 > L5$b [1] 5 > # You don't need quotes for this method of indexing, BUT you also now can't use variable names to index the dictionary like you could by indexing with strings > s <- "b" > L5[[s]] [1] 5 > L5$s NULL > > # You can get all the keys in a list (dictionary), using the names() function > names(L5) [1] "a" "b" "c" > # You can change the keys by using the names() as well > names(L5)[1] <- "w" > L5 $w [1] 4 $b [1] 5 $c [1] 6 > L5[["w"]] [1] 4 > L5$w [1] 4 > L5$a NULL > # As usual, you can change the value for a key in place > L5[["w"]] <- c(76, 54) > L5 $w [1] 76 54 $b [1] 5 $c [1] 6 > # IMPORTANT: keys should be *unique*. You should not have two keys with teh same name in one list. > # R let's you have two keys with the same name, but when you index it, you only get the *first* value with that key, not both. > # It also doesn't help you look up a value. The whole point is to look up one value with one key > L6 <- list(a=2, b=3, a=4) > L6[["a"]] [1] 2 > # ^ Not what we want > # Keys *must* be unique! > > > > L <- list() > append(L, c(1,2,3)) [[1]] [1] 1 [[2]] [1] 2 [[3]] [1] 3 > append(L, list(c(1,2,3))) [[1]] [1] 1 2 3 > source('~/Desktop/list_functionss.R') > ListOfSums(c(1,2,3), c(4,5,6)) [[1]] [1] 1 4 5 [[2]] [1] 2 5 7 [[3]] [1] 3 6 9 > source('~/Desktop/list_functionss.R') > AddToVectors(list(c(1,2), c(3,4)), 7) [[1]] [1] 1 2 7 [[2]] [1] 3 4 7 > source('~/Desktop/list_functionss.R') > PrintKeysAndValues(list(a=5, b=6, c=7)) Key: a Value: 5 Key: b Value: 6 Key: c Value: 7 > list(a=5, b=6, c=7) $a [1] 5 $b [1] 6 $c [1] 7