12.07.2015 Views

R dummies

R dummies

R dummies

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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Similar to the way that R displays vectors, [[1]] means that R is showing thefirst element of a list. Lists are extremely important concepts in R; they allowyou to combine all kinds of variables. You can read more about lists in Chapter7.In the preceding example, this list has only a single element. Yes, that’s right:The list has one element, but that element is a vector.To extract an element from a list, you have to use double square brackets.Split your pangram into words, and assign the first element to a new variable calledwords, using double-square-brackets ([[]]) subsetting, as follows:words words[1] “The” “quick” “brown” “fox” “jumps” “over” “the” “lazy” “dog”To find the unique elements of a vector, including a vector of text, you usethe unique() function. In the variable words, “the” appears twice: once inlowercase and once with the first letter capitalized. To get a list of the uniquewords, first convert words to lowercase and then use unique:> unique(tolower(words))[1] “the” “quick” “brown” “fox” “jumps” “over” “lazy”[8] “dog”Concatenating textNow that you’ve split text, you can concatenate these elements so that theyagain form a single text string.Changing text caseTo change some elements of words to uppercase, use the toupper()function:toupper(words[c(4, 9)])[1] “FOX” “DOG”To change text to lowercase, use tolower():> tolower(“Some TEXT in Mixed CASE”)

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

Saved successfully!

Ooh no, something went wrong!