12.07.2015 Views

R dummies

R dummies

R dummies

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

[1] 2> nchar(x)[1] 5 6Notice that this time, R tells you that your vector has length 2 and that thefirst element has five characters and the second element has six characters.Extracting a subset of a vectorYou use the same indexing rules for character vectors that you use fornumeric vectors (or for vectors of any type). The process of referring to asubset of a vector through indexing its elements is also called subsetting. Inother words, subsetting is the process of extracting a subset of a vector.To illustrate how to work with vectors, and specifically how to create subsets,we use the built-in datasets letters and LETTERS. Both are character vectorsconsisting of the letters of the alphabet, in lowercase (letters) and uppercase(LETTERS). Try it:> letters[1] “a” “b” “c” “d” “e” “f” “g” “h” “i” “j” “k”[12] “l” “m” “n” “o” “p” “q” “r” “s” “t” “u” “v”[23] “w” “x” “y” “z”> LETTERS[1] “A” “B” “C” “D” “E” “F” “G” “H” “I” “J” “K”[12] “L” “M” “N” “O” “P” “Q” “R” “S” “T” “U” “V”[23] “W” “X” “Y” “Z”Aside from being useful to illustrate the use of subsets in this chapter, youcan use these built-in vectors whenever you need to make lists of things.Let’s return to the topic of creating subsets. To extract a specific element froma vector, use square brackets. To get the tenth element of letters, for example,use the following:> letters[10][1] “j”To get the last three elements of LETTERS, use the following:

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!