How to Use Mathematical Functions in R
In R, of course, you want to use more than just basic operators. R comes with a whole set of mathematical functions. R naturally contains a whole set of functions that you’d find on a technical calculator as well. All these functions are vectorized, so you can use them on complete vectors.
|Function||What It Does|
|abs(x)||Takes the absolute value of x|
|log(x,base=y)||Takes the logarithm of x with base y; if base is not specified, returns the natural logarithm|
|exp(x)||Returns the exponential of x|
|sqrt(x)||Returns the square root of x|
|factorial(x)||Returns the factorial of x (x!)|
|choose(x,y)||Returns the number of possible combinations when drawing y elements at a time from x possibilities|
How to calculate logarithms and exponentials in R
In R, you can take the logarithm of the numbers from 1 to 3 like this:
> log(1:3)  0.0000000 0.6931472 1.0986123
Whenever you use one of these functions, R calculates the natural logarithm if you don’t specify any base.
You calculate the logarithm of these numbers with base 6 like this:
> log(1:3,base=6)  0.0000000 0.3868528 0.6131472
For the logarithms with bases 2 and 10, you can use the convenience functions log2() and log10().
You carry out the inverse operation of log() by using exp(). This last function raises e to the power mentioned between brackets, like this:
> x <- log(1:3) > exp(x)
Again, you can add a vector as an argument, because the exp() function is also vectorized. In fact, in the preceding code, you constructed the vector within the call to exp(). This code is yet another example of nesting functions in R.
Scientific notation in R
Scientific notation allows you to represent a very large or very small number in a convenient way. The number is presented as a decimal and an exponent, separated by e. You get the number by multiplying the decimal by 10 to the power of the exponent. The number 13,300, for example, also can be written as 1.33 × 10^4, which is 1.33e4 in R:
> 1.33e4  13300
Likewise, 0.0412 can be written as 4.12 × 10^–2 , which is 4.12e-2 in R:
> 4.12e-2  0.0412
R doesn’t use scientific notation just to represent very large or very small numbers; it also understands scientific notation when you write it. You can use numbers written in scientific notation as though they were regular numbers, like so:
> 1.2e6 / 2e3  600
R automatically decides whether to print a number in scientific notation. Its decision to use scientific notation doesn’t change the number or the accuracy of the calculation; it just saves some space.
How to use trigonometric functions in R
All trigonometric functions are available in R: the sine, cosine, and tangent functions and their inverse functions. You can find them on the Help page you reach by typing ?Trig.
So, you may want to try to calculate the cosine of an angle of 180 degrees like this:
> cos(120)  0.814181
This code doesn’t give you the correct result, however, because R always works with angles in radians, not in degrees. Pay attention to this fact; if you forget, the resulting bugs may bite you hard in the, er, leg.
Instead, use a special variable called pi. This variable contains the value of — you guessed it — π (3.141592653589 . . .).
The correct way to calculate the cosine of an angle of 120 degrees, then, is this:
> cos(120*pi/180)  -0.5