Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 894
Martijn Avatar asked Mar 13 '23 16:03

Martijn


1 Answers

You can use cumsum to check wether there are more "start" values than "stop" values to know if the process is running or not:

res <- cbind(myvec, c("", "running")[(cumsum(myvec=="start") > cumsum(myvec=="stop")) + 1])

you can add "running" for lines with "stop" afterwards:

res[res[, 1]=="stop", 2] <- "running"

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

data

myvec <- c("", "", "start", "", "", "stop", "", "", "start", "stop", "")
like image 200
Cath Avatar answered Mar 16 '23 06:03

Cath