47

Are there build-in functions in R for the conversion of radians to degree and degree to radians?

So far I wrote my one own functions:

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

#test:
rad2deg(pi) #180
rad2deg(2*pi) #360
deg2rad(180) #pi
Uwe Keim
  • 38,279
  • 56
  • 171
  • 280
Iris
  • 992
  • 2
  • 9
  • 22
  • 2
    `install.packages("sos", dependencies = TRUE); library(sos); findFn("convert degree to radian")`. –  Sep 03 '15 at 08:22
  • Very useful! Thank you @Pascal – Iris Sep 03 '15 at 08:30
  • 3
    I am a little surprised that after all these years, R doesn't have a built in function to convert between degree and radian! Must we install extension to support it? At this point, I'd rather take OP's solution to achieve what I need – Antony Sep 20 '17 at 20:08

3 Answers3

12

The comment of Pascal was very useful and I found several ones, e.g.

install.packages("NISTunits", dependencies = TRUE)
library(NISTunits)

NISTdegTOradian(180)
NISTradianTOdeg(pi)
Iris
  • 992
  • 2
  • 9
  • 22
10

You can use the units package for this.

library(units)
pi_rad <- as_units(pi, "radians")
pi_deg <- set_units(pi_rad, "degrees")
set_units(pi_deg, "radians")
jsta
  • 2,890
  • 25
  • 32
0

If you have a data.frame It could help you

In my case davis_2$wd is the column in degree

#Add column to Data Base
davis_2$radian_wd = davis_2$wd

#Create a loop to change the data, and change the 62'th col to Radians
for(i in 1:nrow(davis_2)){
    davis_2[i, 62] = (davis_2[i, 62]*pi)/180
}
# Review
head(davis_2$radian_wd)
Edwin Torres
  • 119
  • 2
  • 6