我試圖建立一個函數來計算矢量運算的大小和方向 爲了解決逆π函數被限制在pi/2域內的問題, -pi/2,如果e小於0,我需要向e添加2pi。但是,一旦添加了if else行,我會在「}」中遇到「Error:unexpected'}'。任何人都可以向我解釋爲什麼?錯誤:「}」中出現意外的'}'
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)
}
這不是問題@nico你必須指定2pi爲2 * pi – The6thSense