In this tutorial, we will look at the common trigonometric functions in the R programming language with the help of some examples.

## How to compute the sine, cosine, and tan of a value in R?

R comes with built-in methods for common trigonometric functions such as sine, cosine, and tan. The following is the syntax for using these functions.

`sin(x)`

– Compute the sine of the value passed.`cos(x)`

– Compute the cosine of the value passed.`tan(x)`

– Compute the tan, which is defined as`sin(x)/cos(x)`

.

Note that you must pass the value in radians to the above trigonometric functions.

## Examples

Let’s look at examples of using the above functions in R.

### The `sin()`

function in R

You can apply the `sin()`

function directly to a value in radians. For example, let’s get the sine of 30 degrees, which is π/6 in radians.

# compute sine of π/6 print(sin(pi/6))

Output:

[1] 0.5

You can see that we get the sine of π/6 as 0.5. Note that here we use the built-in `pi`

constant available in R to represent π.

You can also apply the `sin()`

function to a vector in R. In this case, the `sin()`

function gets applied to each value in the vector. Let’s look at an example.

# compute sine of a vector vec <- c(0, pi/6, pi/4, pi/3, pi/2) print(sin(vec))

Output:

[1] 0.0000000 0.5000000 0.7071068 0.8660254 1.0000000

We get the sine of each value in the above vector.

### The `cos()`

function in R

You can similarly apply the `cos()`

function to get the cosine of a value. Again, the value must be in radians. For example, let’s compute the cosine of π.

# compute cosine of π print(cos(pi))

Output:

[1] -1

You can see that we get the cosine of π as -1.

You can also apply the `cos()`

function to a vector in R.

# compute cosine of a vector vec <- c(0, pi/6, pi/4, pi/3, pi/2) print(cos(vec))

Output:

[1] 1.000000e+00 8.660254e-01 7.071068e-01 5.000000e-01 6.123234e-17

We get the cosine of each value in the above vector.

### The `tan()`

function in R

In trigonometry, tan(x) is defined as sin(x)/cos(x). You can use the `tan()`

function in R to compute the tan of a value. Its usage is similar to the `sin()`

and the `cos()`

functions in R. Pass the value in radians for which you want to compute the tan.

Let’s look at an example – let’s compute the tan of 45 degrees which is π/4 in radians.

# compute tan of π/4 print(tan(pi/4))

Output:

[1] 1

We get the tan of π/4 as 1.

You can also apply the `tan()`

function to a vector in R.

# compute tan of a vector vec <- c(0, pi/6, pi/4, pi/3, pi/2) print(tan(vec))

Output:

[1] 0.000000e+00 5.773503e-01 1.000000e+00 1.732051e+00 1.633124e+16

We get the tan of each value in the above vector.

You might also be interested in –

**Subscribe to our newsletter for more informative guides and tutorials. ****We do not spam and you can opt out any time.**