[R] grouping

R. Michael Weylandt michael.weylandt at gmail.com
Tue Apr 3 21:11:45 CEST 2012


Please take a look at my first reply to you:

ave(y, findInterval(y, quantile(y, c(0.33, 0.66))))

Then read ?ave for an explanation of the syntax. ave takes two
vectors, the first being the data to be averaged, the second being an
index to split by. You don't want to use split() here.

Michael

On Tue, Apr 3, 2012 at 2:50 PM, Val <valkremk at gmail.com> wrote:
> I did look at it the result  is below,
>
> x=c(46, 125 , 36 ,193, 209, 78, 66, 242 , 297,45 )
>
> #lapply( split(x, cut(x, quantile(x, prob=c(0, .333, .66 ,1)) ,
> include.lowest=TRUE) ), mean)
>  ave( split(x, cut(x, quantile(x, prob=c(0, .333, .66 ,1)) ,
> include.lowest=TRUE) ), mean)
>
>> ave( split(x, cut(x, quantile(x, prob=c(0, .333, .66 ,1)) ,
> include.lowest=TRUE) ), mean)
> $`[36,74]`
> [1] NA
>
> $`(74,197]`
> [1] NA
>
> $`(197,297]`
> [1] NA
>
> There were 11 warnings (use warnings() to see them)
>
>
>
>
>
> On Tue, Apr 3, 2012 at 2:35 PM, Petr Savicky <savicky at cs.cas.cz> wrote:
>
>> On Tue, Apr 03, 2012 at 02:21:36PM -0400, Val wrote:
>> > Hi All,
>> >
>> > On the same data  points
>> > x=c(46, 125 , 36 ,193, 209, 78, 66, 242 , 297,45 )
>> >
>> > I want to have have the following output  as data frame
>> >
>> > x       group   group mean
>> > 46       1        42.3
>> > 125     2        89.6
>> > 36       1        42.3
>> > 193     3        235.25
>> > 209     3        235.25
>> > 78       2        89.6
>> > 66       2        89.6
>> > 242     3        235.25
>> > 297     3        235.25
>> > 45       1        42.3
>> >
>> > I tried the following code
>> >
>> >
>> > dat <- data.frame(xc=split(x, cut(x, quantile(x, prob=c(0, .333, .66
>> ,1))))
>> > gxc <- with(dat, tapply(xc, group, mean))
>> > dat$gxc <- gxce[as.character(dat$group)]
>> > txc=dat$gxc
>> >
>> > it did not work for me.
>>
>> David Winsemius suggested to use ave(), when you asked this
>> question for the first time. Can you have look at it?
>>
>> Petr Savicky.
>>
>> ______________________________________________
>> 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.
>>
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> 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.



More information about the R-help mailing list