# 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 120 degrees like this:

> cos(120) [1] 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) [1] -0.5

Sometimes the result of a calculation is dependent on multiple values in a vector. One example is the sum of a vector; when any value changes in the vector, the outcome is different. This complete set of functions and operators is called the *vector operations.*

Actually, operators are also functions. But it’s useful to draw a distinction between functions and operators, because operators are used differently from other functions. It helps to know, though, that operators can, in many cases, be treated just like any other function if you put the operator between backticks and add the arguments between parentheses, like this:

> `+`(2,3) [1] 5

This may be useful later on when you want to apply a function over rows, columns, or subsets of your data.