overwhelmed - 5 months ago 19

R Question

Say I have the following vector:

`y`

[1] 10

[2] 9

[3] 8

[4] 6

[5] 7

[6] -3

[7] -1

Though the position of the minimum can be get using

`which(y==min(y)`

`6`

`4`

Thanks in advance.

Answer

Try:

```
first.min <- function(y) {
d <- which(diff(y) > 0)
if (length(d) > 0) d[1] else NA
}
first.min(y)
##[1] 4
```

This gives you the position. To get the minimum value:

```
y[first.min(y)]
##[1] 6
```