R user -4 years ago 175

R Question

I create a point with a random rho and theta with the following code:

`set.seed(1)`

rho <- sqrt(runif(1, 0.0, 1.0))

theta <- runif(1, 0, 2*pi)

obtaining

`rho=0.515`

`theta=2.338`

I can get the x and y values doing

`x=rho*cos(theta)`

`y=rho*sin(theta)`

`-0.358`

`0.371`

However, if I'm doing the inverse procedure

`r<-sqrt(x^2+y^2)`

which results the same as rho but doing

`a<-atan(y/x)`

I obtain a different result than theta.

Could you tell what I'm doing wrong?

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

It's better to use `atan2`

```
atan2(y, x)
#[1] 2.338364
```

which is (almost) equal to `theta`

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**

Latest added