I have data that is large enough to need in the first place data.table, and I was very pleased with how much it has been so far. Today I read quite a lot of documentation (of course, almost not all), but I have not found this yet.
I have a data table with the keys placeidand t2one other column t1. What I would like to do is set t1to 0in each row, where t2at a minimum level, on placeid.
## Sample data
set.seed(47)
require(data.table)
dt <- data.table(placeid = rep(letters[1:3], each = 3), t1 = runif(9), t2 = runif(9))
setkeyv(dt, cols=c("placeid", "t2"))
Since it t2is in the key, the line I want to change is the first in each group. I managed to get it to work with the operator ifelse, but is there a better way to do this using an argument i [.data.table?
I was hoping that one of them would work, although, thinking a little more, it makes sense that they do not:
dt[1, t1 := 0, by = placeid]
dt[which.min(t2), t1 := 0, by = placeid]
What I found to work (the result is the desired result):
dt[, t1 := ifelse(t2 == min(t2), 0, t1), by = placeid]