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.

second value of the result.4. A vector with the selected values is returned as the result.Trying it outTo see how this works in the example of the priceCalculator() function, trythe function out at the command line in the console. Say you have two clients andyou worked 25 and 110 hours for them, respectively. You can calculate the netprice with the following code:> my.hours my.hours * 40 * ifelse(my.hours > 100, 0.9, 1)[1] 1000 3960Didn’t you just read that the second and third arguments should be a vector?Yes, but the ifelse() function can recycle its arguments. And that’s exactly what itdoes here. In the preceding ifelse() function call, you translate the logical vectorcreated by the expression my.hours > 100 into a vector containing the numbers 0.9and 1 in lieu of TRUE and FALSE, respectively.Adapting the functionOf course, you need to adapt the priceCalculator() function in such a waythat you also can input a vector with values for the argument public. Otherwise,you wouldn’t be able to calculate the prices for a mixture of public and privateclients. The final function looks like this:priceCalculator

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

Saved successfully!

Ooh no, something went wrong!