cos in R: How to Use cos() Function in R

All trigonometric functions that are available in R are the sine, cosine, and tangent method and their inverse methods.

cos in R

The cos() is a built-in mathematical R function that computes the cosine value of numeric value data. The cos() function takes a numerical value as an argument and returns the cosine value.

To calculate the cosine of a value in R, use the cos() function.

Syntax

cos(x)

Parameters

The cos() function takes as an argument is either numeric or complex vectors.

Example

cos(30)

Output

[1] 0.1542514

This code doesn’t give you the correct result, however, because R always works with angles in radians, not in degrees. So pay attention to the fact; if you forget, the resulting bugs may bite you hard in the er, leg.

The correct way to calculate the cosine of an angle of 30 degrees then uses the following code.

cos(30 * pi / 180)

Output

[1] 0.8660254

Cos value of pi in R

The pi in R is an inbuilt constant. So let’s find the cos() value of the pi constant.

cos(pi)

Output

[1] -1

Let’s pass the -pi.

cos(-pi)

Output

[1] -1

Applying cos() function to a Vector

To create a Vector in R, use the c() function. Then, add three elements of a vector using the pi constant.

rv <- c(pi, pi / 4, pi / 3)
cos(rv)

Output

[1] -1.0000000 0.7071068 0.5000000

That is it for the cos() function in R.

See also

sin in R

acos in R

asin in R

cosh in R

sinh in R

Leave a Comment