[R] mean-aggregate – but use unique for factor variables

Bert Gunter gunter.berton at gene.com
Tue Sep 25 23:44:15 CEST 2012


In the absence of data via dput() or other wise ....

I would assume that if you made your function FUN something like:

function(x) if(is.factor(x)) unique(x) else mean(x)

(or maybe length(unique(x)) as first clause)

you'd get what you want.  No?

On Tue, Sep 25, 2012 at 2:32 PM, Martin Batholdy
<batholdy at googlemail.com> wrote:
> Hi,
>
> I have a data.frame which I want to aggregate.
> There are some grouping variables and some continuous variables for which I would like to have the mean.
>
> However there are also some factor-variables in the data-frame that are not grouping variables and I actually would like to aggregate these variables with the unique() function.
>
> Is that possible with the standard aggregate-function?
>
> If I use it like this:
>
> aggregate(data, by=list(data$grouping1, data$grouping2), FUN=mean)
>
> I get vectors of NAs for the other factor-variables in the data.frame.
> But I actually want the unique value …
>
> Is that possible?
>
>
> thanks!
> ______________________________________________
> 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.



-- 

Bert Gunter
Genentech Nonclinical Biostatistics

Internal Contact Info:
Phone: 467-7374
Website:
http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm




More information about the R-help mailing list