[R] Which hist cell each value falls in?

David Winsemius dwinsemius at comcast.net
Fri Dec 18 14:21:01 CET 2009


On Dec 18, 2009, at 2:57 AM, Jim Lemon wrote:

> On 12/18/2009 06:35 AM, Doug Hill wrote:
>> Hi, all. I'm using hist() to obtain a vector of break values in an  
>> interval.
>> I then want to be able to identify which cell any value from  
>> another vector
>> falls in.
>>
>> E.g. applying
>>
>>
>>> breaks
>>>
>>  [1] -3.5 -3.0 -2.5 -2.0 -1.5 -1.0 -0.5  0.0  0.5  1.0  1.5  2.0
>>
>> to
>>
>>
>>> x
>>>
>>  [1] -3.74519666 -0.38183630 -1.22884247 -0.20971824 -0.30533939  
>> -0.36271207
>> [7] -2.27513499 -2.23688653 -1.98827155 -1.48666274 -1.26155084  
>> -0.15234555
>> [13] -0.09497287  0.34488440
>>
>> would give
>>
>>
>>> xcells
>>>
>>  [1] 1  8  6  8  8  8  4  4  5  6  6  8  8  9
>>
>> where:
>>                   x<= breaks[1] ->  cell 1
>> breaks[1]<  x<= breaks[2] ->  cell 2
>> breaks[2]<  x<= breaks[3] ->  cell 3, etc.

 > breaks <- scan()
1: -3.5 -3.0 -2.5 -2.0 -1.5 -1.0 -0.5  0.0  0.5  1.0  1.5  2.0
13:
Read 12 items
 > xcells <- scan()
1: -3.74519666 -0.38183630 -1.22884247 -0.20971824 -0.30533939  
-0.36271207
7: -2.27513499 -2.23688653 -1.98827155 -1.48666274 -1.26155084  
-0.15234555
13:  -0.09497287  0.34488440
15:
Read 14 items

 > breaks <- c(-Inf,breaks,Inf)
 > findInterval(xcells, breaks)
  [1] 1 8 6 8 8 8 4 4 5 6 6 8 8 9

-- 
David
>>
>>
> Hi Doug,
> The function below does more or less what you want, except that the  
> bins are numbered from zero (meaning that a value is below the range  
> of bins as x[1] is) to length(breaks) (meaning that a value is above  
> the range of bins).
>
> whichBin<-function(x,breaks,right=TRUE) {
> lenx<-length(x)
> # any leftovers must be out of range
> wb<-rep(lenx,lenx)
> if(right) wb[x<=breaks[1]]<-0
> else wb[x<breaks[1]]<-0
> for(bin in 1:(length(breaks)-1)) {
>  if(right) wb[x>breaks[bin] & x<=breaks[bin+1]]<-bin
>  else wb[x>=breaks[bin] & x<breaks[bin+1]]<-bin
> }
> return(wb)
> }
>
> Jim
>
> ______________________________________________
> 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
Heritage Laboratories
West Hartford, CT




More information about the R-help mailing list