[R] Binning Data and Event rates

David L Carlson dcarlson at tamu.edu
Tue Apr 18 05:50:43 CEST 2017


After creating ppdat and ppdat$Valbin, aggregate() will get you the churn proportions:

> aggregate(Churn~Valbin, ppdat, mean)
       Valbin     Churn
1 (20.9,43.7] 0.8333333
2 (43.7,66.3] 0.0000000
3 (66.3,89.1] 0.5000000


David L. Carlson
Department of Anthropology
Texas A&M University


-----Original Message-----
From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Jim Lemon
Sent: Monday, April 17, 2017 4:59 PM
To: prateek pande <prtkpande at gmail.com>
Cc: r-help mailing list <r-help at r-project.org>
Subject: Re: [R] Binning Data and Event rates

Hi Pateek,
Try this:
ppdat<-read.table(text="Values Churn
 21          1
 22          1
 31.2       1
 32          1
 35          0
 43          1
 45           0
 67          1
 67           0
 76           0
 89           1",
 header=TRUE)
ppdat$Valbin<-cut(ppdat$Values,breaks=c(20.9,43.7,66.3,89.1))
binPct<-function(x) return(100*sum(x)/length(x))
binnedPct<-by(ppdat$Churn,ppdat$Valbin,binPct)
bpctdf<-data.frame('Binned data'=names(binnedPct),
 'churn%'=as.vector(binnedPct))
bpctdf

Jim

On Tue, Apr 18, 2017 at 5:20 AM, prateek pande <prtkpande at gmail.com> wrote:
> I have a data, in the form mentioned below.
>
> Values Churn
> 21          1
> 22          1
> 31.2       1
> 32          1
> 35          0
> 43          1
> 45           0
> 67          1
> 67           0
> 76           0
> 89           1
>
> Now i want to bin the values variables into bins and corresponding that
> want the churn percentage, like mentioned below
>    Binned data  churn%
>   (20.9,43.7]    0.83
>   (43.7,66.3]    0
>   (66.3,89.1]    0.50
>
> Please help
>
> « Return to Rcom-l <http://r.789695.n4.nabble.com/Rcom-l-f930477.html>  |  3
> v
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org 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.

______________________________________________
R-help at r-project.org 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.


More information about the R-help mailing list