我有一些data.table
,其数量列如下:
n = 1e5
set.seed(1)
dt <- data.table(id = 1:n, amount = pmax(0,rnorm(n, mean = 5e3, sd = 1e4)))
给出了一个断点矢量:
breaks <- as.vector( c(0, t(sapply(c(1, 2.5, 5, 7.5), function(x) x * 10^(1:4))) ) )
对于这些中断定义的每个时间间隔,我想使用data.table
语法:
amount
包含amount
的计数等于或大于左边界(基本为n * (1-cdf(amount))
对于1,这主要起作用,但不会为空间隔返回行:
dt[, .N, keyby = breaks[findInterval(amount,breaks)] ] #would prefer to get 0 for empty intvl
2,我试过了:
dt[, sum(amount >= thresh[.GRP]), keyby = breaks[findInterval(amount,breaks)] ]
但它没有用,因为sum
仅限于群组内,而不是超出群组。因此提出了一种解决方法,它也返回空的间隔:
dt[, cbind(breaks, sapply(breaks, function(x) sum(amount >= x)))] # desired result
那么,data.table
修复我的方法是什么?并获得两者的空间隔?
答案 0 :(得分:5)
我会考虑这样做:
mybreaks = c(-Inf, breaks, Inf)
dt[, g := cut(amount, mybreaks)]
dt[.(g = levels(g)), .N, on="g", by=.EACHI]
g N
1: (-Inf,0] 30976
2: (0,10] 23
3: (10,25] 62
4: (25,50] 73
5: (50,75] 85
6: (75,100] 88
7: (100,250] 503
8: (250,500] 859
9: (500,750] 916
10: (750,1e+03] 912
11: (1e+03,2.5e+03] 5593
12: (2.5e+03,5e+03] 9884
13: (5e+03,7.5e+03] 9767
14: (7.5e+03,1e+04] 9474
15: (1e+04,2.5e+04] 28434
16: (2.5e+04,5e+04] 2351
17: (5e+04,7.5e+04] 0
18: (7.5e+04, Inf] 0
如果您想要CDF,可以使用cumsum
。