10.07.2015 Views

Using R for Introductory Statistics : John Verzani

Using R for Introductory Statistics : John Verzani

Using R for Introductory Statistics : John Verzani

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>Using</strong> R <strong>for</strong> introductory statistics 6The result of sin (pi) is the very small 1.225· 10 –16 and not precisely 0, as it should be.Such numeric differences are not uncommon. The logarithm † has base e <strong>for</strong> its defaultbehavior.* The full order of operations is covered in the help page <strong>for</strong> Syntax.† This book has a few examples where logarithms and exponentials are important, although <strong>for</strong> themost part knowledge of these function is not essential <strong>for</strong> following the material.Many functions in R have extra arguments that allow us to change the defaultbehavior. For example, to use base 10 <strong>for</strong> the logarithm, we could use either of thefollowing:> log(10,10)[1] 1> log(10, base=10)[1] 1To understand the first one, log(10, 10), we need to know that R expects the base to bethe second argument of the function. The second example uses a named argument, of thetype base=, to say explicitly that the base is 10. The first style contains less typing; thesecond is easier to remember and read. This book will typically use named arguments <strong>for</strong>clarity.Warnings and errors When R finds a command it doesn’t understand, it will respondwith an error message. For example:> squareroot(2)Error: couldn’t find function “squareroot”> sqrt 2Error: syntax error> sqrt(−2)[1] NaNWarning message:NaNs produced in: sqrt(−2)> sqrt(2 # the +, like >, is nottyped+ )[1] 1.414The first command produced an Error: and no output, as R could not find a function withthat name. The second command failed to use parentheses around the argument, causingR to issue a syntax error. Parentheses are required to use a function. We typeset functionnames with parentheses to remind us of that. The third command produced an output, butR guessed it wasn’t what we wanted. The output NaN means “not a number,” in responseto the request <strong>for</strong> a square root of a negative number. Consequently, a Warning wasprinted. The last command shows what happens if R encounters a line that is notcomplete. The continuation prompt, +, is printed, indicating more input is expected.

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

Saved successfully!

Ooh no, something went wrong!