In the exercises below we cover the basics of lists. Before proceeding, first read section 6.1-6.2 of An Introduction to R, and the help pages for the sum, length, strsplit, and setdiff functions.
5.1.1.1 If: p <- c(2,7,8), q <- c(“A”, “B”, “C”) and x <- list(p, q), then what is the value of x[2]?
p <- c(2,7,8)
q <- c("A", "B", "C")
x <- list(p, q) # Second element of the list is q. So x[2] is q <- "A" "B" "C".
x[2]
## [[1]]
## [1] "A" "B" "C"
5.1.1.2 If w <- c(2, 7, 8), v <- c(“A”, “B”, “C”) and x <- list(w, v), then which R statement will replace “A” in x with “K”.
w <- c(2, 7, 8)
v <- c("A", "B", "C")
x <- list(w, v) #"A" is the first element of v, which is the second element of list x. Careful about the square brackets.
x[[2]][1] <- "K"
x
## [[1]]
## [1] 2 7 8
##
## [[2]]
## [1] "K" "B" "C"
5.1.1.3 If a <- list (“x”=5, “y”=10, “z”=15), which R statement will give the sum of all elements in a?
a <- list ("x"=5, "y"=10, "z"=15)
sum(unlist(a)) # You have to unlist items to sum up.
## [1] 30
5.1.1.4 If Newlist <- list(a=1:10, b=“Good morning”, c=“Hi”), write an R statement that will add 1 to each element of the first vector in Newlist.
Newlist <- list(a=1:10, b="Good morning", c="Hi")
Newlist$a = Newlist$a +1
Newlist$a #Or Newlist[1]
## [1] 2 3 4 5 6 7 8 9 10 11
5.1.1.5 If b <- list(a=1:10, c=“Hello”, d=“AA”), write an R expression that will give all elements, except the second, of the first vector of b.
b <- list(a=1:10, c="Hello", d="AA")
b$a[-2]
## [1] 1 3 4 5 6 7 8 9 10
5.1.1.6 Let x <- list(a=5:10, c=“Hello”, d=“AA”), write an R statement to add a new item z = “NewItem” to the list x.
x <- list(a=5:10, c="Hello", d="AA")
x$z <- "NewItem"
5.1.1.7 Consider y <- list(“a”, “b”, “c”), write an R statement that will assign new names “one”, “two” and “three” to the elements of y.
y <- list("a", "b", "c")
names(y) <- c("one","two","three")
5.1.1.8 If x <- list(y=1:10, t=“Hello”, f=“TT”, r=5:20), write an R statement that will give the length of vector r of x.
x <- list(y=1:10, t="Hello", f="TT", r=5:20)
length(x$r)
## [1] 16
5.1.1.9 Let string <- “Grand Opening”, write an R statement to split this string into two and return the following output:
#[[1]]
#[1] "Grand"
#[[2]]
#[1] "Opening"
string <- "Grand Opening"
#Expected output looks like a print of list which has 2 elements and each of them have one character element inside.
#First, start with splitting "string"
string.splitted <- strsplit(string," ") #It gives you a list of 1 with 2 elements. We need a list of 2.
mylist <- list(string.splitted[[1]][1], string.splitted[[1]][2])
mylist
## [[1]]
## [1] "Grand"
##
## [[2]]
## [1] "Opening"
string.splitted # try this to compare outputs
5.1.1.10 Let y <- list("a", "b", "c") and q <- list("A", "B", "C", "a", "b", "c"). Write an R statement that will return all elements of q that are not in y, with the following result:
[[1]]
[1] "A"
[[2]]
[1] "B"
[[3]]
[1] "C"
y <- list("a", "b", "c")
q <- list("A", "B", "C", "a", "b", "c")
setdiff(q, y)
## [[1]]
## [1] "A"
##
## [[2]]
## [1] "B"
##
## [[3]]
## [1] "C"