atan2 in R: How to Use atans2() Function in R

The atan2 is a mathematical function that returns the arctangent of the two numbers x and y. It is similar to calculating the arctangent of y / x, except that the signs of both arguments are used to determine the quadrant of the result.

atan2 in R

The atan2(y, x) is a built-in R function that returns the radian arctangent between the x-axis and the vector from the origin to (x, y). The atan2 is an obvious trigonometric function. The trigonometric functions compute the cosine, sine, tangent, arc-cosine, arc-sine, arc-tangent, and the two-argument arc-tangent.


atan2(y, x)


y: It is a Raster* object.

x: It is a Raster* object.


Let’s calculate the atan2 value of 1.

atan2(2, 1)


[1] 1.107149

Applying atan2() function to a Vector

To create a Vector in R, use the c() function. Then pass that vector to the atan2() function.

y <- c(3, 4)
x <- c(1, 2)

atan2(y, x)


[1] 1.249046 1.107149

Let’s see another example.

x <- c(4, 5, 6)
dt <- atan2(1, x)


[1] 0.2449787 0.1973956 0.1651487

Passing a pi to the atan2() function

The pi is an inbuilt constant in R programming, and its value is 3.141593.

Let’s find the pi constant’s atan2() value.

x <- c(4, 5, 6)

dt <- atan2(pi / 3, x)



[1] 0.2560528 0.2064553 0.1727924

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

See also

R atanh()

R tanh()

R sinh()

Leave a Comment