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
On 09/18/2013 10:43 PM, ioanna ioannou wrote:> 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 > } > } > } >Hi Ioanna, Does: ids<-runif(1) bin<-which(ids<pro)[1]-1 do what you want? Jim
On 18-09-2013, at 14:43, ioanna ioannou <ii54250 at msn.com> wrote:> 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."Doesn't seem to be working" is pretty vague. It either works or it doesn't. What are you seeing? An error message (I got one)? Wrong result?> Any ideas? >Yes. In the expression "j in 1:length(pro[,i])-1" the 1 is subtracted from the left and righthand side of :. The expression should read: j in 1:(length(pro[,i])-1)> for (i in 1:2){ > ids<-runif(1) > for (j in 1:length(pro[,i])-1){ > if (ids < pro[j,i]) { > ds[i]<-jYou need to break after this.> } > else { > ds[i]<-6 > } > } > } >Alternative to Jim's suggestion is: have a look at findInterval. Berend> Best, > IOanna > > ______________________________________________ > 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.
Hi,
Try:?set.seed(49)
?t(sapply(seq_len(ncol(pro)),function(i) {ids<- runif(1);
x1<-cut(ids,breaks=pro[,i]); c(ids=ids,bin_location=x1)}))
#?????????? ids bin_location
#[1,] 0.3656991??????????? 1
#[2,] 0.4878542??????????? 2
A.K.
----- Original Message -----
From: ioanna ioannou <ii54250 at msn.com>
To: r-help at r-project.org
Cc:
Sent: Wednesday, September 18, 2013 8:43 AM
Subject: Re: [R] Identifying the bin where a value is included.
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
______________________________________________
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.