How to Convert Radians to Degrees in R

Radians and degrees are both units used for measuring angles. The circle is comprised of 2π radians, which is the equivalent of 360°. We often come across the scenarios in mathematical calculations in which we need to convert degrees to radians and radians to degrees. Fortunately, R provides packages that do this exactly. Let’s see how to do it.

Convert Radians to Degrees in R

To convert radian values to degrees in R, use the rad2deg() function. The rad2deg() is a grid library’s function that calculates radians from degrees.

Syntax

rad2deg(rad)

Parameters

The rad2deg() method accepts a number in radian.

Example

To work with the rad2deg() function, you must install the REdaS package in your system.

Install REdaS package in R

The next step will be to create an R file and import the library using the following code.

library(REdaS)

Now, we can use the rad2deg() function to convert radian to degree values.

Take three-radian values and convert them to degrees.

library(REdaS)

rad2deg(pi)
rad2deg(3.14)
rad2deg(22 / 7)

The pi is a built-in constant in R.

Output

Loading required package: grid
[1] 180
[1] 179.9087
[1] 180.0724

As you can see that the degree values of pi, 3.14, and 22/7 are 180, 179.9087, and 180.0724.

Converting radians to degrees using units package

There is another third-party package called units that provides a function that will convert the radian values to degrees.

Converting radians to degrees using units package

The next step is to import the units library using the following code.

library(units)

Now, we can use the as_units() and set_units() functions to convert radian to degree values.

library(units)

pi_rad <- as_units(pi, "radians")
pi_deg <- set_units(pi_rad, "degrees")
pi_deg

Output

180 [°]

And we get the 180* value from the radian value.

Writing your own functions to convert radians to degrees.

Although it is always preferred to use the built-in or library-provided functions for conversion but, out of curiosity, you can create your own functions too. Let’s see how to create it.

Converting radians to degrees

rad2deg <- function(rad) {
 (rad * 180) / (pi)
}

Now, you can call the function and pass the values in the form of a radian, and it will return the value in degree.

rad2deg <- function(rad) {
 (rad * 180) / (pi)
}

rad2deg(pi)
rad2deg(3.14)
rad2deg(22 / 7)

Output

[1] 180
[1] 179.9087
[1] 180.0724

Converting degrees to radians

Let’s create a custom function in R that converts degrees to radians.

deg2rad <- function(deg) {
 (deg * pi) / (180)
}

Now, you can call the function and pass the values in the form of degree, and it will return the value in radian.

deg2rad <- function(deg) {
 (deg * pi) / (180)
}

deg2rad(180)
deg2rad(179.9087)
deg2rad(180.0724)

Output

[1] 3.141593
[1] 3.139999
[1] 3.142856

That is it for converting radians to degrees in R tutorial.

Leave a Comment