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:
How can I do this in an elegant way with R?
Thank you very much on beforehand for your input!
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", "")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With