[R] column selection for aggregate()

Petr PIKAL petr.pikal at precheza.cz
Tue Jan 19 10:07:41 CET 2010


Hi

If I really wanted aggregate all numerics by all non numerics this is how 
I would do it

my.numerics <- which(sapply(zeta, is.numeric))
my.factor <- which(sapply(zeta, is.factor))
aggregate(zeta[, my.numerics], zeta[, my.factor], mean)

Regards
Petr



r-help-bounces at r-project.org napsal dne 18.01.2010 16:33:17:

> I didn't understand from the help what really does the function rowMeans 

> but it looks like it doesn't take into account the categorical variables 

> (I want to calculate the means when the values of all categorical 
> variables are the same, second part of aggregate). Moreover, ssfa_num 
> contains only numeric variables, meaning that the categories will not be 

> associated with it.
> I'm kind of confused with this approach.
> You think it would work for me?
> 
> Thanks
> Ivan
> 
> b k a écrit :
> >
> >
> > On Mon, Jan 18, 2010 at 10:17 AM, Ivan Calandra 
> > <ivan.calandra at uni-hamburg.de <mailto:ivan.calandra at uni-hamburg.de>> 
> > wrote:
> >
> >     Thanks for your answer, but it doesn't work...
> >
> >     Here is what I get:
> >     > ssfamean <- aggregate(ssfa[[10:24]],ssfa[c("SPECSHOR", "BONE",
> >     "TO_POS", "FACETTE", "SHEARFAC", "ENA_BA")],mean)
> >     Error in .subset2(x, i, exact = exact) :
> >       recursive indexing failed at level 2
> >
> > 
> > Wouldn't you be better off with rowMeans() ? Split your dataframe into 

> > numeric matrix:
> > 
> > ssfa_num  <- ssfa[10:24]
> > 
> > ssfameans <- rowMeans(ssfa_num)
> > 
> >
> >     Also col_index <- match("Asfc", ssfa) doesn't really work since
> >     col_index is composed of 1227 NAs...
> >
> >
> > 
> >
> > 
> > Yes, it should be:
> > 
> > col_index <- match("Asfc", names(ssfa))
> > 
> > Ben
> 
>    [[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