jeudi 7 janvier 2016

R- Conditional calculation based on reference to values in other row

I would like to make a reference to a value in the same column on another row. I have one column with start and stop moment. I would like to add a column that indicates if the machine is running or not.

This is how the data looks like:

c("", "", "start", "", "", "stop", "", "", "start", "stop", "")

The output should look like this:

 [1,] ""      ""       
 [2,] ""      ""       
 [3,] "start" "running"
 [4,] ""      "running"
 [5,] ""      "running"
 [6,] "stop"  "running"
 [7,] ""      ""       
 [8,] ""      ""       
 [9,] "start" "running"
[10,] "stop"  "running"
[11,] ""      ""      

In the second column I would like to do:

  • IF same row first column equals start --> running
  • ELSE IF previous row first column equals stop --> ""
  • ELSE the same value as the previous row for the second column

How can I do this in an elegant way with R?

Thank you very much on beforehand for your input!

Aucun commentaire:

Enregistrer un commentaire