AppendixEssentials of the R Language
R as a Calculator
The command line after the screen prompt > is an excellent calculator:
> log(42/7.3)
[1] 1.749795
By default, logs in R are base e (natural or Napierian, not base 10 logs), but you can specify any base you want, as a second argument to the logfunction. Here is log base 2 of 16:
> log(16,2)
[1] 4
Each line can have many characters, but if you want to evaluate a complicated expression, you might like to continue it on one or more further lines for clarity. The way you do this is by ending the line at a place where the line is obviously incomplete (e.g. with a trailing comma, operator, or with more left parentheses than right parentheses, implying that more right parentheses will follow). When continuation is expected, the line prompt changes from > to +
> 5+6+3+6+4+2+4+8+
+ 3+2+7
[1] 50
Note that the + continuation prompt does not carry out arithmetic plus. If you have made a mistake, and you want to get rid of the + prompt and return to the > prompt, then press the Esc key and use the Up arrow to edit the last (incomplete) line.
From here onwards (and throughout the book), the prompt character > is omitted. The material that you should type on the command line is shown in red font in this book. Just press the Return key to see the answer. The output from R is shown in dark blue Courier New font, which uses absolute rather than proportional spacing, so that columns of numbers remain neatly aligned on the page and on the screen. ...
Get Statistics: An Introduction Using R, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.