if statement - R- Conditional calculation based on values in other row and column -
my data has following format: - first column: indication if machine running - second column: total time machine running
see here below 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 add third column gives total time machine has been running (by adding times since machine started run). see here below 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 write code in r in elegant way, programming skills limited moment. there knows solution problem? thank on beforehand!
first transform data more appropriate data structure can contain mixed data types:
m <- 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"))) df <- as.data.frame(m, stringsasfactors = false) df[] <- lapply(df, type.convert, as.is = true)
then can package data.table:
library(data.table) setdt(df) df[, total := cumsum(v), = rleid(c)] df[c == "", total := 0] # c v total # 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
Comments
Post a Comment