[R] Identifying the bin where a value is included.

ioanna ioannou ii54250 at msn.com
Wed Sep 18 14:43:23 CEST 2013


Hello all, 

A very simple problem. 

Lets assume I  have an interval [0,1] and I split it in 6 bins having
thresholds:
pro= cbind(0, 0.3675509, 0.8618615, 0.9814291, 0.9975283, 0.9997789,
1.0000000,
           0, 0.3662881, 0.8609743, 0.9812032, 0.9974822, 0.9997738,
1.0000000)

dim(pro)<-c(7,2)

 I randomly generate a number and I want to identify which bin it belongs
to. How? What I provide below doesn't seem to be working. 
Any ideas?

for (i in 1:2){
    ids<-runif(1)
  for (j in 1:length(pro[,i])-1){
    if (ids < pro[j,i]) {
      ds[i]<-j
    }
    else {
      ds[i]<-6
    } 
  }
}

Best, 
IOanna



More information about the R-help mailing list