sin, cos, and tan trigonometric functions in R

Common Trigonometric Functions in R

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

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 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.


Authors

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

  • Gottumukkala Sravan Kumar
Scroll to Top