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.
Syntax
atan2(y, x)
Parameters
y: It is a Raster* object.
x: It is a Raster* object.
Example
Let’s calculate the atan2 value of 1.
atan2(2, 1)
Output
[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)
Output
[1] 1.249046 1.107149
Let’s see another example.
x <- c(4, 5, 6)
dt <- atan2(1, x)
print(dt)
Output
[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)
print(dt)
Output
[1] 0.2560528 0.2064553 0.1727924
That is it for the atan2() function in R.
See also

Krunal Lathiya is an Information Technology Engineer by education and web developer by profession. He has worked with many back-end platforms, including Node.js, PHP, and Python. In addition, Krunal has excellent knowledge of Data Science and Machine Learning, and he is an expert in R Language. Krunal has written many programming blogs, which showcases his vast expertise in this field.