mardi 20 décembre 2016

loop to satisfy multiply conditions and copy values

Hey guys i have a data frame with 4 million rows and my main goal is to get cycle data out of the DF. The data contains an ID for each vehicle, how much load it can carry and the speed. Each vehicle can have as many as 10 cycles. A cycle starts when speed and load are zero and ends when the load is of a certain value and speed changes from zero to some value.

ID Speed Load 1 0 0
1 0 20
1 0 40
1 10 40
1 20 40 . . . . . .

2 0 0 2 0 10 2 0 40 . . . . . .

What i want R to do condition 1) R should find where both speed and load =0. It should take 5 Rows before this row and copy the whole data frame until: condition 2) 5 rows after speed changes from zero to some value and payload is loaded. After this table is copied i need it to be stored in a separate DF and the process should repeat itself. If possible can R create a separate column with the cycle number?

Basically instead of doing this in Excel by finding each cycle manually i would like R to find each cycle for me.

Thank you all for the help. It would be a great help to solve this problem.

Aucun commentaire:

Enregistrer un commentaire