cos() Function in R with Example

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 an inbuilt mathematical R function that computes the cosine value of numeric value data. 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. 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. 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. 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 cos() function in R language.

See also

R log

NULL in R

NaN in R

Leave a Comment