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.

third game, and so on.The vector returned by diff() is always one element shorter than theoriginal vector you gave as an argument.The rule about missing values applies here, too. When your vector contains amissing value, the result from that calculation will be NA. So, if you calculate thedifference with the vector x, you get the following result:> diff(x)[1] 3 -4 NA NABecause the fourth element of x is NA, the difference between the third andfourth element and between the fourth and fifth element will be NA as well. Justlike the cumulative functions, the diff() function doesn’t have an argument toeliminate the missing values.Recycling argumentsIn Chapter 3 and earlier in this chapter, we mention recycling arguments. Takea look again at how you calculate the total amount of money Granny and Geraldineraised (see “Using arithmetic operators,” earlier in this chapter) or how youcombine the first names and last names of three siblings (see Chapter 3). Eachtime, you combine a vector with multiple values and one with a single value in afunction. R applies the function, using that single value for every value in thevector. But recycling goes far beyond these examples.Any time you give two vectors with unequal lengths to a recycling function,R repeats the shortest vector as often as necessary to carry out the task youasked it to perform. In the earlier examples, the shortest vector is only onevalue long.Suppose you split up the number of baskets Granny made into two-pointersand three-pointers:> Granny.pointers

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

Saved successfully!

Ooh no, something went wrong!