在数据表中计算每个分组中等于最大值的记录数。

huangapple go评论64阅读模式
英文:

Count Records Equal to Max in Data.Table within group

问题

我想计算在每个组中有多少行等于最大值(在data.table中)。我知道如何从https://stackoverflow.com/questions/24558328/select-the-row-with-the-maximum-value-in-each-group中找到data.table中的最大值。

set.seed(5)
z <- data.table( data.frame(
    group=( sample( letters[1:5], 25,  replace=T) ),
    size=( sample( 1:5, 25,  replace=T) )))

z <- z[ order( group, size ), ]

z[ , maxsize := max(size), by = .(group)]

所以在这个例子中,我想要一个等于2的列,用于组a(因为有两个观察值等于3),以及组b的4,组c的2等等。

英文:

I want to count how many rows are equal to the max value within a group (in data.table). I know from https://stackoverflow.com/questions/24558328/select-the-row-with-the-maximum-value-in-each-group how to find the max value in data.table.

set.seed(5)
z &lt;- data.table( data.frame(
	group=( sample( letters[1:5], 25,  replace=T) ),
	size=( sample( 1:5, 25,  replace=T) )))

z &lt;- z[ order( group, size ), ]

z[ , maxsize := max(size), by = .(group)]

So in this example, I want a column equal to 2 for group a, (since there are two observations equal to 3) and 4 for b, 2 for c etc.

在数据表中计算每个分组中等于最大值的记录数。

答案1

得分: 0

当逻辑向量求和时,TRUE/FALSE值将被视为1/0,因此按组对size == max(size)求和。

z[, count_max := sum(size == max(size)), by = group]

或者,使用基本的R语言,我们可以使用ave

transform(z, count_max = ave(size, group, FUN = \(x) sum(x == max(x)))
英文:

When a logical vector is summed the TRUE/FALSE values will be regarded as 1/0 so sum size == max(size) by group.

z[, count_max := sum(size == max(size)), by = group]

Alternately, in base R we could use ave

transform(z, count_max = ave(size, group, FUN = \(x) sum(x == max(x))))

huangapple
  • 本文由 发表于 2023年3月3日 22:56:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/75628606.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定