I have earth flow displacements in two directions, converted to point data, which I am trying to combine for an overall offset direction (point to where the flow is going). I have magnitude in a separate raster file.
I'm trying to convert an old excel code into R so I can work with larger datasets. I have an IF statement in excel which will convert 2 displacements into direction in Cartesian co-ordinates using atan2. I know that Excel and R have opposite argument placements for atan2, so I've swapped them.
Where it gets messy is after atan2, when I need to convert to degrees and "bin" the results in a correct quadrant. Ultimately, I'd like the values in Cartesian coordinates (0 to 360), rather than -180 to 180 (radians).
I've tried running through these already asked questions, but am still lost:
How to map atan2() to degrees 0-360
Reshaping EPA wind speed & direction data with dcast in R
Here is my excel code:
(N2 = the displacement in north direction)
(E2 = the displacement in east direction)
=IF(
(ATAN2(N2,E2)*180/PI())<0, 360+(ATAN2(N2,E2)*180/PI()),
ATAN2(N2,E2)*180/PI()
)
I tried running this in R like this:
theta_rad <- atan2(E,N)
if(theta_rad < 0)
theta_rad +2*pi
theta_deg <- theta_rad*(180/pi)` # Convert to degrees
I've also tried the following, which just crashes R
dir <- (theta_rad > 0 ? x : (2*pi + theta_rad)) * 360 / (2*pi)
Warnings: The condition has length >1 and only the first element will be used.
ifelse(theta_deg <0, 360 + theta_deg , theta_deg)
wheretheta_deg <-atan2(E,N)*180/pi
– maydin2*pi
vice360
for a circle). – r2evans?
notation for R, where is that defined? – r2evans%%
in R), not anifelse
. For example,(360+c(-180,-90,0,90,180)) %% 360
. – r2evans