R Tutorial R Charts & Graphs R Statistics R Resources
Python Java C++ C C# PHP R SQL DS Algo InterviewQ

R - acos() Function



The R acos() function returns arc cosine of a value. The return value will be in the range 0 through 𝜋. In special cases it returns the following:

  • If the argument is NaN or its absolute value is greater than 1, then the result is NaN.

Note: aacos() is the inverse of acos().

Syntax

acos(x)

Parameters

x Required. Specify column to compute on.

Return Value

Returns the arc cosine of the value.

Example:

In the example below, acos() function is used to find out the arc cosine of the value.

#operating on single element atomic vector
print(acos(-0.5))
print(acos(0))
print(acos(0.5))

cat("\nOperating on vector\n")
#operating on vector
v <- c(0, 0.25, 0.5)
print(acos(v))

cat("\nOperating on matrix\n")
#operating on matrix
m <- matrix(c(-1, -0.5, 0, 0.5, 1, NaN), nrow=2)
print(acos(m))

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

The output of the above code will be:

[1] 2.094395
[1] 1.570796
[1] 1.047198

Operating on vector
[1] 1.570796 1.318116 1.047198

Operating on matrix
         [,1]     [,2] [,3]
[1,] 3.141593 1.570796    0
[2,] 2.094395 1.047198  NaN

Operating on first column of matrix
[1] 3.141593 2.094395

❮ R Math Functions

5