data.table:将行值与组值进行比较

时间:2021-09-29 22:47:45

I need to do the following operation on a huge data set. So, is there a more direct / more performant way of checking whether a row value has a specific relation to its corresponding group? Following is an example, where I want to check by row whether the row value of column is the smallest for a given group and mark that in new

我需要在庞大的数据集上执行以下操作。那么,是否有更直接/更高效的方法来检查行值是否与其对应的组具有特定的关系?下面是一个示例,我想按行检查列的行值是否为给定组的最小值,并在新的内容中标记

# df is a data.table
# first get the minimum value per group
df[, Min:=min(column), by=list(idx1, idx2)]
df[, new:=month==Min]
df[, Min:=NULL]

1 个解决方案

#1


1  

No reproducible example in OP, but my guess is this would work:

OP中没有可重现的例子,但我的猜测是可行的:

df[, new := (month == min(column)), by = list(idx1, idx2)]

#1


1  

No reproducible example in OP, but my guess is this would work:

OP中没有可重现的例子,但我的猜测是可行的:

df[, new := (month == min(column)), by = list(idx1, idx2)]