PigWolf - 9 months ago 56

R Question

I have a bearing of X degrees and I wish to calculate other bearings either side of X. We all know a circle has 360 degrees but if I'm given a bearing of 0 degrees and I wish to calculate the bearings + and - 30 degrees of 0 how can I program this such that I am looking for 0 - 30 = 330 degs and 0 + 30 = 30 degrees?

Answer

The modulus operator (`%%`

) can help you with that

```
x <- 0
(x+30) %% 360
# [1] 30
(x-30) %% 360
# [1] 330
```

In this case it will keep all values in the range `[0, 360)`

. See the `?"%%"`

help page for more information or even the wikipedia page for these types of operations

Source (Stackoverflow)