[R] Rank samples by breaks in hist and assign result as factor

David Winsemius dwinsemius at comcast.net
Wed Dec 7 05:47:24 CET 2011


On Dec 6, 2011, at 7:25 PM, Frostygoat wrote:

> Hi R users,
>
> My goal is to rank my samples according to how they fall out in a
> histogram with 10 bins to produce a ranking for each sample according
> to where it falls on the histogram, with a "1" to represent one tail
> of the hist, a "10" to represent the other tail, and a "5" for the
> median/mean. I have a number of different data sets to do this with
> and in all cases I need this ranking between1-10 regardless of the
> number of samples in each set.
>
> e.g.,
>
> x=rnorm(50)
> h=hist(x,breaks=10)
> h$breaks
>
> [1] -2.0 -1.5 -1.0 -0.5  0.0  0.5  1.0  1.5  2.0
>
> So, I want to add a factor to x where if x is less than -2.0, then new
> factor="1"; if x is greater than -2.0 but x is less than -1.5, then
> new factor="2", etc.

 > xb <- as.numeric(cut(x, h$breaks))
 > xb
  [1] 9 2 6 9 9 9 6 5 4 7 8 6 6 9 4 8 5 4 9 7 5 9 5 5 4 2 6 7 4 6 4 9  
3 8 5 3 6 1 7
[40] 5 7 9 4 4 4 7 8 5 6 7

 > str(xdf)
'data.frame':	50 obs. of  2 variables:
  $ x    : num  1.577 -1.525 0.254 1.54 1.6 ...
  $ xbrks: num  9 2 6 9 9 9 6 5 4 7 ...



>
> rank doesn't seem to help
>
> Any tips?
>
> Thanks much.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list