I was exploring alternatives of how to do row operations in data.table and I think I've found a bug.
These three lines of code should return the same result. However, the result of by = .I seems return a wrong result.
dt[, sdd := sum(.SD[, 2:4, with=FALSE]), by = 1:NROW(dt) ]
dt[, rowpos := .I][ , sdd := sd(.SD[, -1, with=FALSE]), by = rowpos ]
dt[ , sdd := sd(.SD[, -1, with=FALSE]), by = .I ]
sample data:
dt <- data.table(V0 =LETTERS[c(1,1,2,2,3)], V1=1:5, V2=3:7, V3=5:1)
I was exploring alternatives of how to do row operations in
data.tableand I think I've found a bug.These three lines of code should return the same result. However, the result of
by = .Iseems return a wrong result.dt[, sdd := sum(.SD[, 2:4, with=FALSE]), by = 1:NROW(dt) ]dt[, rowpos := .I][ , sdd := sd(.SD[, -1, with=FALSE]), by = rowpos ]dt[ , sdd := sd(.SD[, -1, with=FALSE]), by = .I ]sample data:
dt <- data.table(V0 =LETTERS[c(1,1,2,2,3)], V1=1:5, V2=3:7, V3=5:1)