[R] mean for every quartile
Ulrik Stervbo
ulrik.stervbo at gmail.com
Mon May 16 15:41:10 CEST 2016
Hi Elahe,
you can create a matrix of ranges and loop over each row:
m <- c(384, 384, 384, 384, 512, 384, 384, 320, 320, 320, 320, 320, 320,
320, 320, 320, 320, 384,
384, 384, 320, 320, 320, 320, 384, 384, 256, 320, 320, 320, 384, 320, 320,
320, 384, 384,
320, 320, 320, 320, 320, 320, 320, 320, 320, 384, 320, 320, 320, 320, 320,
320, 384, 320,
320, 320, 320, 320, 320, 320, 384, 512, 320, 320, 320, 320, 320, 320, 320,
384, 384, 320,
320, 320, 384, 320, 320, 320, 320, 256, 320, 320, 384, 320, 384, 320, 384,
320, 320, 320,
384, 320, 320, 320, 320, 320, 320, 320, 320, 320, 320, 320)
qm <- quantile(m)
q.ranges <- matrix(c(0, qm[1:(length(qm) - 1)], qm), ncol = 2)
apply(q.ranges, MARGIN = 1, function(cr){
mean(m[m > cr[1] & m <= cr[2]])
})
Hope this helps
Ulrik
On Mon, 16 May 2016 at 15:32 Michael Dewey <lists at dewey.myzen.co.uk> wrote:
> Dear Elahe
>
> In line
>
> On 16/05/2016 13:31, ch.elahe via R-help wrote:
> > Hi all,
> > I have a column in my df and I want to get quartiles for this column and
> then calculate mean for each and every quartile, here is my column:
> >
>
> The quartiles are strictly speaking the boundaries but if you really
> meant that the problem is trivial so i assume you want to cut the
> variable at the quartiles.
>
> >
> > df$BR
> > [1] 384 384 384 384 512 384 384 320 320 320 320 320 320 320 320 320
> 320 384
> > [19] 384 384 320 320 320 320 384 384 256 320 320 320 384 320 320 320
> 384 384
> > [37] 320 320 320 320 320 320 320 320 320 384 320 320 320 320 320 320
> 384 320
> > [55] 320 320 320 320 320 320 384 512 320 320 320 320 320 320 320 384
> 384 320
> > [73] 320 320 384 320 320 320 320 256 320 320 384 320 384 320 384 320
> 320 320
> > [91] 384 320 320 320 320 320 320 320 320 320 320 320
> >
> > I do the following to get the quartiles:
> >
> >
> > quantile(m$BR)
> > 0% 25% 50% 75% 100%
> > 256 320 320 368 512
> >
> > now how can I get mean for each quartile?
>
> How about setting up a vector which takes the values 1, 2, 3, 4
> depending on the values of BR with cutpoints defined by
> quantile(BR)(using ifelse) and then using tapply?
>
> > Thnaks for any help,
> > Elahe
> >
> > ______________________________________________
> > 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.
> >
>
> --
> Michael
> http://www.dewey.myzen.co.uk/home.html
>
> ______________________________________________
> 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.
>
[[alternative HTML version deleted]]
More information about the R-help
mailing list