1

Apologies if this is an obvious one.

I have a numeric vector

nv <- round(rnorm(100, 100, 10), 0)

And then I have a separate set of bins

bins <- seq(90, 110, 5)

I want to recode the nv vector into the most proximate bin value (in the real data, the bins are not so neatly organized.)

Thanks

tomw
  • 3,114
  • 4
  • 29
  • 51
  • "in the real data, the bins are not so neatly organized" - Are the bins evenly spaced (constant width) or not? – Floris Nov 13 '13 at 03:45
  • Google "R histogram unequal bins". There are many solutions... I suspect that their approach can be applied to this situation. I know that in Matlab the function `histc` will do exactly what you want; if you can find the R equivalent you're home and dry. – Floris Nov 13 '13 at 03:47
  • 1
    It looks that either `findInterval` or `cut` (see http://stackoverflow.com/a/13482987/1967396) will get you very close to a solution. – Floris Nov 13 '13 at 03:59

1 Answers1

0

Floris was exactly right with their suggestion of histc --the pracma library has a seemingly identical function.

tomw
  • 3,114
  • 4
  • 29
  • 51