rmathdegreesradians

Radians to degrees with a roundoff value between 0-360 degress


I want to convert radians to degrees with a round off function. The degrees should come between 0-360 for all the radian values.

Say, the value I want to convert is 300(in radians)

So,

300*57.3 = 17190 degrees

No. of rotation on the plane = 17190/360 = 47.75

I wrote a radtodeg function :

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

How should I round off a degree equivalent to 17190 between 0 to 360.

Anyone with the round off function, please?


Solution

  • Use the modulus operator %% in your function if you want the answer to be modulo 360 (i.e. always between 0 and 360)

    radtodeg <- function(rad) {((rad * 180) / (pi)) %% 360}
    
    radtodeg(pi)
    #> [1] 180
    
    radtodeg(2 * pi)
    #> [1] 0
    
    radtodeg(3 * pi)
    #> [1] 180
    
    radtodeg(17190)
    #> [1] 314.4498