53

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
  • 39,551
  • 56
  • 175
  • 291
Iris
  • 1,072
  • 3
  • 10
  • 22
  • 2
    `install.packages("sos", dependencies = TRUE); library(sos); findFn("convert degree to radian")`. –  Sep 03 '15 at 08:22

3 Answers3

13

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
  • 3,216
  • 25
  • 35
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
  • 1,072
  • 3
  • 10
  • 22
-1

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
  • 117
  • 2
  • 6
  • 2
    It's unnecessary to write a loop: `davis_2$radian_wd <- (davis_2$radian_wd*pi)/180` is enough – Iris Jun 12 '16 at 17:27