stats134711 - 5 months ago 60

R Question

It seems that for large

`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

I'm trying to obtain quantiles of my data, with

`m`

`cut2`

`N`

Answer

Is this what you want?

```
cut3 = function(x, m) {
p = seq(0, 1, by = m / length(x))
q = quantile(x, probs = p, names = F)
result = cut(x, breaks = q)
}
```

Testing it out:

```
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]
```