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.

**Data Science Programs By Skill Level**

**Introductory** ⭐

- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science

**Intermediate ⭐⭐⭐**

- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization

**Advanced ⭐⭐⭐⭐⭐**

- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science

**🔎 Find Data Science Programs 👨💻 111,889 already enrolled**

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

# 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.**