I am trying to build a function to calculate the magnitude and direction of vector operation. In order to solve the problem that inverse trig function is restricted within the domain of pi/2 and -pi/2, I need to add 2pi to the e if e is less than 0. But I encounter the "Error: unexpected '}' in "}" once I added the if else line. Would anyone be able to explain to me why?
vectorMD <- function(x){
c=x[1:2]-x[3:4]
mt <- function(x) {round(sqrt(sum(x^2)),1)}
d <- round(mt(c),1)
e <- round(atan(c[2]/c[1]),1)
if(e < 0){e <- e+2*2pi
return(e)}
paste("magnitude =",d,"direction =",e)
}