[R] grouping

Petr Savicky savicky at cs.cas.cz
Tue Apr 3 20:35:37 CEST 2012


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.



More information about the R-help mailing list