# [R] Histogram + % of cases for a given criteria

S. Nunes snunes at gmail.com
Thu May 14 19:43:00 CEST 2009

```Hi,

Thanks for the contribution. I tested the code but I got a couple or errors.
I'll try to debug these in the next days.

Maybe there is a simpler approach to what I am trying to do.
I have a bunch of measures and a column with the success of the case (1/0).

I simply want to evaluate my measures and see how each one impacts the
success rate.
My first idea was to have a histogram for each measure superimposed
with the % of success for each bar.

I am open to suggestions on how to address this.

Thanks again,
--
Sergio Nunes

2009/5/14 Jim Lemon <jim at bitwrit.com.au>:
> S. Nunes wrote:
>>
>> Hi all,
>>
>> I am doing some explorations using a dataset with the following
>> structure (id, value, flag).
>> For instance:
>>
>> a, 2.2, 1
>> b, 3.0, 1
>> c, 2.9, 0
>> d, 3.1, 1
>> ...
>>
>> I have plotted a standard histogram using a simple command like:
>> hist(data\$value)
>>
>> My question:
>>
>> I would like to superimpose a line ([0%-100%] scale) representing the
>> % of values that, for each class of the histogram, have the \$flag
>> equal to 1.
>>
>>
>> What strategy
>>
>> I hope I made myself clear. Please let me know if not.
>>
>>
>
> Hi Sergio,
> A curly one indeed. Try this:
>
> crit_cuts<-function(x,breaks,right=TRUE,crit_var,crit) {
> ncats<-length(breaks)-1
> critcount<-rep(0,ncats)
> for(i in 1:ncats) {
>  if(right) inside<-x >= breaks[i] & x < breaks[i+1]
>  else inside<-x > breaks[i] & x <= breaks[i+1]
>  critcount[i]<-sum(crit_var[inside]==crit)
> }
> return(critcount)
> }
> x<-rnorm(30)+5
> crit<-sample(0:1,30,TRUE)
> histinfo<-hist(x)
> critcount<-crit_cuts(x,histinfo\$breaks,crit_var=crit,crit=1)
> segments(histinfo\$breaks[1:(length(histinfo\$breaks)-1)],
>  critcount,histinfo\$breaks[-1],critcount,lty=2,col="red")
>
> Jim
>
>

```