似乎对于大型N
(比如说2e6
)cut2
包中的Hmisc
函数会抛出错误
y = cut2(rnorm(2000000,0,1),m=sqrt(2000000))
Error in if (cj == upper) next : missing value where TRUE/FALSE needed
In addition: Warning message:
In (1:g) * nnm : NAs produced by integer overflow
我正在尝试获取我的数据的分位数,每个分位数中有m
个点,并且还记录每个分位数的端点。 cut2
执行此操作,但对于大型N
不太好。还有更好的选择吗?
答案 0 :(得分:1)
这是你想要的吗?
cut3 = function(x, m) {
p = seq(0, 1, by = m / length(x))
q = quantile(x, probs = p, names = F)
result = cut(x, breaks = q)
}
测试出来:
x = rnorm(2e6)
m = sqrt(2e6)
qq = cut3(x, m)
summary(as.numeric(table(qq)))
# Min. 1st Qu. Median Mean 3rd Qu. Max.
# 1414 1414 1414 1414 1414 1415
head(qq)
# [1] (0.4757,0.4779] (-1.021,-1.018] (0.4325,0.4344] (1.376,1.381] (-2.156,-2.138] (0.1215,0.1233]
# 1414 Levels: (-4.964,-3.196] (-3.196,-2.981] (-2.981,-2.86] (-2.86,-2.766] (-2.766,-2.696] (-2.696,-2.637] ... (3.145,3.607]