jeudi 7 janvier 2016

R- Conditional calculation based on values in other row and column

My data has the following format: - first column: indication if the machine is running - second column: total time that the machine is running

See here below the dataset:

structure(c("", "running", "running", "running", "", "", "", 
"running", "running", "", "10", "15", "30", "2", "5", "17", "47", 
"12", "57", "87"), .Dim = c(10L, 2L), .Dimnames = list(NULL, 
    c("c", "v")))

I would like to add a third column that gives the total time that the machine has been running (by adding all the times since the machine started to run). See here below the desired output:

 [1,] ""        "10" "0"   
 [2,] "running" "15" "15"  
 [3,] "running" "30" "45"  
 [4,] "running" "2"  "47"  
 [5,] ""        "5"  "0"   
 [6,] ""        "17" "0"   
 [7,] ""        "47" "0"   
 [8,] "running" "12" "12"  
 [9,] "running" "57" "69"  
[10,] ""        "87" "0" 

I tried to write some code in R to get this in an elegant way, but my programming skills are too limited for the moment. Is there anybody that knows a solution for this problem? Thank you on beforehand!

Aucun commentaire:

Enregistrer un commentaire