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

Info icon This 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!
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 ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern