1

似乎对于大型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. 有更好的选择吗?

4

1 回答 1

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]
于 2016-09-14T22:57:20.483 回答