Pregunta

I need to find the min value between two rows in my data set, for every row. So this would be, the min value in rows 1&1, 1&2, 2&3 etc. I need the first row of the new array to be 0 (corresponding to the first value of the array) to get a new array of the same length.

This is easier for subtraction... e.g. Cl <- abs(diff(data$Cl,lag=1)), however I can't seem to get this for the min value.

Here is my data:

cl
0.000
0.297
0.000
1.067
2.484
4.851
7.471
12.671
16.221
17.141
18.021
19.011
20.581
22.561
22.131
19.731
15.161
9.261
9.311
1.602
0.000

Advice/hints welcome!

¿Fue útil?

Solución

Use pmin, head and tail:

with(data, c(Cl[1], pmin(head(Cl, -1), tail(Cl, -1))))
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top