{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Week 4.pdf - Week 4 Wednesday 1:10 AM Lists We can't...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
We can't combine strings and numbers into a vector without converting numbers into strings > list (123, "fred", 456) [[1]] [1] 123 [[2]] [1] "fred" [[3]] [1] 456 We CAN use a list to keep them intact: > list (1:4, 3:10) [[1]] [1] 1 2 3 4 [[2]] [1] 3 4 5 6 7 8 9 10 Lists can contain anything, including vectors of different lengths (and lists within lists) > L <- list (c(3,1,7), c("red","green"), 1:4) > L[[2]] [1] "red" "green" > L[[3]] <-c("x","y","z") >L[[3]] [1] "x" "y" "z" [[ … ]] operators single element of list Lists: > L <- list (c("a","b"), 2:4, c("x","y","z")) > v <- character(0) # creates a string vector with zero strings > for (i in 1:length(L)) v <- c (v, L[[i]]) > v [1] "a" "b" "2" "3" "4" "x" "y" "z" The following will create a single vector of strings, called v , containing all the elements of all the vectors from the list L: Note how we can start with a vector with no elements, and then extend it using the c function. Vector of numbers was automatically converted to a vector of strings, so they could be combined with a string vector Extending Lists: > b <- a < 30 > if (b) cat("It’s TRUE! \n") It’s TRUE!
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}