R数据表:将条件值与行值比较 [英] R data table: compare row value to group values, with condition
本文介绍了R数据表:将条件值与行值比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是问题的延续:
现在:
x = data.table( id=c(1,1,1,1,1,1,1,1), price = c(10, 10, 12, 12, 12, 15,
8, 11), subgroup = c(1, 1, 1, 1, 1, 1, 2, 2))
id price subgroup
1: 1 10 1
2: 1 10 1
3: 1 12 1
4: 1 12 1
5: 1 12 1
6: 1 15 1
7: 1 8 2
8: 1 11 2
$ b b
并希望计算每个ID的价格较低的行数,但只计算子组1中的行数
and would like to calculate the number of rows with lower prices per id, but only counting the ones in subgroup 1.
如果我使用:
x[,cheaper := rank(price, ties.method="min")-1, by=id]
结果是:
> x
id price subgroup cheaper
1: 1 10 1 1 # only 1 is cheaper (row 7)
2: 1 10 1 1 # only 1 is cheaper (row 7)
3: 1 12 1 4 # 4 frows are cheaper (row 1,2,7,8)
4: 1 12 1 4 # etc
5: 1 12 1 4
6: 1 15 1 7
7: 1 8 2 0
8: 1 11 2 3
但我想要的结果是:
> x
id price subgroup cheaper_in_subgroup_1
1: 1 10 1 0 # nobody in subgroup 1 is cheaper
2: 1 10 1 0 # nobody in subgroup 1 is cheaper
3: 1 12 1 2 # only row 1 and 2 are cheaper in subgroup 1
4: 1 12 1 2
5: 1 12 1 2
6: 1 15 1 5
7: 1 8 2 0 # nobody in subgroup 1 is cheaper
8: 1 11 2 2 # only row 1 and 2 are cheaper in subgroup 1
推荐答案
可能有更多的 data.table
ish方式实现,在每个 id
There's probably a more data.table
ish way achieving this, but here an attempt using vapply
within each id
x[, cheaper := vapply(price,
function(x) sum(price[subgroup == 1L] < x),
FUN.VALUE = integer(1L)),
by = id]
x
# id price subgroup cheaper
# 1: 1 10 1 0
# 2: 1 10 1 0
# 3: 1 12 1 2
# 4: 1 12 1 2
# 5: 1 12 1 2
# 6: 1 15 1 5
# 7: 1 8 2 0
# 8: 1 11 2 2
这篇关于R数据表:将条件值与行值比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文