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.

notice that it uses an object control but doesn’t have an argument with that name.How does this work then? When you call the function, the following happens (seeFigure 8-2):1. The function calculate.eff() creates a new local environment thatcontains the objects x (with the value of fifty), y (with the value of hundred),control (with the value of nothing), as well as the function min.base().2. The function min.base() creates a new local environment within the one ofcalculate.eff() containing only one object z with the value of x.3. min.base() looks for the object control in the environment ofcalculate.eff() and subtracts the mean of this vector from every number of z.This value is then returned.4. The same thing happens again, but this time z gets the value of y.5. Both results are divided by another, and the result is passed on to theglobal environment again.The local environment is embedded in the environment where the functionis defined, not where it’s called. Suppose you use addPercent() insidecalculate.eff() to format the numbers. The local environment created byaddPercent() is not embedded in the one of calculate.eff() but in the globalenvironment, where addPercent() is defined.Figure 8-2: The relations between the environments of the functions calculate.eff andmin.base.Dispatching to a MethodWe want to cover one more thing about functions, because you need it tounderstand how one function can give a different result based on the type of valueyou give the arguments. R has a genius system, called the generic function system,that allows you to call different functions using the same name. If you think this is

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

Saved successfully!

Ooh no, something went wrong!