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.

[1] “The quick brown FOX jumps over the lazy DOG”The collapse argument of paste can take any character value. If you want topaste together text by using an underscore, use the following:paste(words, collapse=”_”)[1] “The_quick_brown_FOX_jumps_over_the_lazy_DOG”You can use sep and collapse in the same paste call. In this case, the vectorsare first pasted with sep and then collapsed with collapse. Try this:> paste(LETTERS[1:5], 1:5, sep=”_”, collapse=”---”)[1] “A_1---B_2---C_3---D_4---E_5”What happens here is that you first concatenate the elements of each vectorwith an underscore (that is, A_1, B_2, and so on), and then you collapse the resultsinto a single string with --- between each element.The paste() function takes vectors as input and joins them together. If onevector is shorter than the other, R recycles (repeats) the shorter vector tomatch the length of the longer one — a powerful feature.Suppose that you have five objects, and you want to label them “sample 1”,“sample 2”, and so on. You can do this by passing a short vector with the valuesample and a long vector with the values 1:5 to paste(). In this example, theshorter vector is repeated five times:> paste(“Sample”, 1:5)[1] “Sample 1” “Sample 2” “Sample 3” “Sample 4” “Sample 5”Sorting textWhat do league tables, telephone directories, dictionaries, and the indexpages of a book have in common? They present data in some sorted manner. Datacan be sorted alphabetically or numerically, in ascending or descending order. Likeany programming language, R makes it easy to compile lists of sorted and ordereddata.

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

Saved successfully!

Ooh no, something went wrong!