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.

R makes it easy to sort vectors in either ascending or descending order.Because each column of a data frame is a vector, you may find that you performthis operation quite frequently.Sorting a vector in ascending orderTo sort a vector, you use the sort() function. For example, to sort Populationin ascending order, try this:> sort(some.states$Population)[1] 365 579 2110 2212 2541 3100 3615 4931 8277[10] 21198Sorting a vector in decreasing orderYou also can tell sort() to go about its business in decreasing order. To dothis, specify the argument decreasing=TRUE:> sort(some.states$Population, decreasing=TRUE)[1] 21198 8277 4931 3615 3100 2541 2212 2110 579[10] 365You can access the Help documentation for the sort() function by typing ?sort into the R console.Sorting data framesAnother way of sorting data is to determine the order that elements should bein, if you were to sort. This sounds long winded, but as you’ll see, having thisflexibility means you can write statements that are very natural.Getting the orderFirst, determine the element order to sort state.info$Population in ascending

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

Saved successfully!

Ooh no, something went wrong!