R Tutorial R Charts & Graphs R Statistics R References

R - cos() Function



The R cos() function returns trigonometric cosine of an angle (angle should be in radians). In special cases it returns the following:

  • If the argument is NaN or an infinity, then the result is NaN.

In the graph below, cos(x) vs x is plotted.

Cos Function

Syntax

cos(x)

Parameters

x Required. Specify column to compute on.

Return Value

Returns the trigonometric cosine of specified angles.

Example:

In the example below, cos() function is used to find out the trigonometric cosine of an angle.

#operating on single element atomic vector
print(cos(pi/6))
print(cos(pi/3))
print(cos(pi/2))

cat("\nOperating on vector\n")
#operating on vector
v <- c(pi/6, pi/4, pi/3)
print(cos(v))

cat("\nOperating on matrix\n")
#operating on matrix
m <- matrix(c(pi/6, pi/4, pi/3, pi/2, pi, NaN), nrow=2)
print(cos(m))

cat("\nOperating on first column of matrix\n")
#operating on first column of matrix
print(cos(m[,1]))

The output of the above code will be:

[1] 0.8660254
[1] 0.5
[1] 6.123234e-17

Operating on vector
[1] 0.8660254 0.7071068 0.5000000

Operating on matrix
          [,1]         [,2] [,3]
[1,] 0.8660254 5.000000e-01   -1
[2,] 0.7071068 6.123234e-17  NaN

Operating on first column of matrix
[1] 0.8660254 0.7071068

❮ R Math Functions

5