Efficient Binning

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Efficient Binning

Dan Abner
Hi all,

I have a situation where I have 16 bins. I generate a random number and
then want to know which bin number the random number falls in. Right now, I
am using a serious of 16 if() else {} statements which get very complicated
with the embedded curly braces. Is there a more efficient (i.e., easier)
way to go about this?


boundaries<-(0:16)/16
rand<-runif(1)

Which bin number (1:16) does rand fall in?

Thanks,

Dan

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Efficient Binning

Marc Schwartz-3

> On Jul 14, 2017, at 9:10 AM, Dan Abner <[hidden email]> wrote:
>
> Hi all,
>
> I have a situation where I have 16 bins. I generate a random number and
> then want to know which bin number the random number falls in. Right now, I
> am using a serious of 16 if() else {} statements which get very complicated
> with the embedded curly braces. Is there a more efficient (i.e., easier)
> way to go about this?
>
>
> boundaries<-(0:16)/16
> rand<-runif(1)
>
> Which bin number (1:16) does rand fall in?
>
> Thanks,
>
> Dan


See ?findInterval

> findInterval(rand, boundaries)
[1] 6

There are various function arguments as well, relative to how to deal with the interval boundaries, as described on the help page.

Regards,

Marc Schwartz

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Efficient Binning

Jeff Newmiller
In reply to this post by Dan Abner
?cut

cut( 0.51, boundaries )

You can also use as.integer to convert the resulting factor to an integer.
--
Sent from my phone. Please excuse my brevity.

On July 14, 2017 7:10:59 AM PDT, Dan Abner <[hidden email]> wrote:

>Hi all,
>
>I have a situation where I have 16 bins. I generate a random number and
>then want to know which bin number the random number falls in. Right
>now, I
>am using a serious of 16 if() else {} statements which get very
>complicated
>with the embedded curly braces. Is there a more efficient (i.e.,
>easier)
>way to go about this?
>
>
>boundaries<-(0:16)/16
>rand<-runif(1)
>
>Which bin number (1:16) does rand fall in?
>
>Thanks,
>
>Dan
>
> [[alternative HTML version deleted]]
>
>______________________________________________
>[hidden email] mailing list -- To UNSUBSCRIBE and more, see
>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.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Loading...