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.

Sorting multiple columns in mixed orderYou may start to wonder how to calculate the order when some of thecolumns need to be in increasing order and others need to be in decreasingorder.To do this, you need to make use of a helper function called xtfrm(). Thisfunction transforms a vector into a numeric vector that sorts in the sameorder. After you’ve transformed a vector, you can take the negative toindicate decreasing order.To sort some.states into decreasing order of region and increasing order ofpopulation, try the following:> index some.states[index, ]Region Population IncomeAlaska West 365 6315Arizona West 2212 4530Colorado West 2541 4884California West 21198 5114Delaware South 579 4809Arkansas South 2110 3378Alabama South 3615 3624Georgia South 4931 4091Florida South 8277 4815Connecticut Northeast 3100 5348Traversing Your Data with the Apply FunctionsR has a powerful suite of functions that allows you to apply a functionrepeatedly over the elements of a list. The interesting and crucial thing about thisis that it happens without an explicit loop. In Chapter 9, you see how to use loopsappropriately and get a brief introduction to the apply family.Because this is such a useful concept, you’ll come across quite a few differentflavors of functions in the apply family of functions. The specific flavor of apply()depends on the structure of data that you want to traverse:Array or matrix: Use the apply() function. This traverses either the rows orcolumns of a matrix, applies a function to each resulting vector, and returns avector of summarized results.

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

Saved successfully!

Ooh no, something went wrong!