r - Apply which.min to data.table under a condition -
i have data.table
, need know index of row containing minimal value under given condition. simple example:
dt <- data.table(i=11:13, val=21:23) # val # 1: 11 21 # 2: 12 22 # 3: 13 23
now, suppose i'd know in row val
minimal under condition i>=12
, 2 in case.
what didn't work:
dt[i>=12, which.min(val)] # [1] 1
returns 1, because within dt[i>=12]
first row.
also
dt[i>=12, .i[which.min(val)]] # [1] 1
returned 1, because .i
supposed used grouping.
what did work:
to apply .i
correctly, added grouping column:
dt[i>=12, g:=true] dt[i>=12, .i[which.min(val)], by=g][, v1] # [1] 2
note, g
na
i<12
, which.min
excludes group result.
but, requires computational power add column , perform grouping. productive data.table
has several millions of rows , have find minimum often, i'd avoid computations.
do have idea, how efficiently solve this?
you can use fact which.min
ignore na values "mask" values don't want consider:
dt[,which.min(ifelse(i>=12, val, na))]
as simple example of behavior, which.min(c(na, 2, 1))
returns 3, because 3rd element min among non-na values.
Comments
Post a Comment