Contents
 |
Introduction
To R |
Simple R Arithmetic
You can use R to evaluate some simple expressions. For example:
> 1+2+3 Let's add these
[1] 6 the answer, labelled with a [1]
> 2+3*4 Slightly more complex...
[1] 14 multiplication is done first
> 3/2+1
[1] 2.5 as is division
> 4*3**3 Use ** or ^ for powers.
[1] 108 and powers are done before * and /
[The [1] looks a bit odd but its function will be clear later!]
R also provides the sort of functions you find on a calculator:
> sqrt(2)
[1] 1.414214
> sin(3.14159) sin(Pi radians) is zero
[1] 2.65359e-06 and this is close...
It also provides Pi as a constant. Try this:
> sin(pi)
[1] 1.224606e-16 much closer to zero...
Here's a short list of some of the arithmetic functions in R:
Name |
Operation |
sqrt |
square root |
abs |
absolute (positive) value |
sin cos tan |
trig functions |
asin acos atan |
inverse trig functions |
sinh cosh tanh |
hyperbolic functions |
asinh acosh atanh |
inverse hyperbolic functions |
exp log |
exponential and natural logarithm |
log10 |
base-10 log |
gamma lgamma |
gamma function and its natural log |
|
These functions can be nested and combined to make more complex expressions:
> sqrt(sin(45*pi/180))
[1] 0.8408964
Contents