英文:
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 <- 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)]
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))))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论