## Find the value of x corresponding to the maximum or minimum of y

You have two numeric vectors, x and y, of the same length and want to find the value of x where y attains its maximum value

**Solution:**
Combine the `which.max`

or `which.min`

function with indexing.

```
> x <- seq(0, pi/2, .2)
> y <- sin(2*x)
> x[which.max(y)]
[1] 0.8
```

See rule 2.5 in *The R Primer* for more information.

Back to tips.