[R] Mean calculation of groups

Rui Barradas ruipbarradas at sapo.pt
Tue Sep 25 17:17:10 CEST 2012


Hello,

Or ?tapply.
ave will return a vector with the length of the input, tapply just one 
value per group.


ave(test$Score, test$Name, FUN = mean)  # 24 values
tapply(test$Score, test$Name, FUN = mean)  # 3 values

Hope this helps,

Rui Barradas

Em 25-09-2012 15:54, Bert Gunter escreveu:
> Nico:
>
> 1. Thanks for the example.
>
> 2. Please read -- or at least peruse -- the tutorial, An Introduction
> to R (or other tutorial that you might find on the Web, there are
> many) to familiarize yourself with R's features.
>
> 3. This is an example of what the apply() family of functions can do,
> so you might want to find out about that (Google is your friend here).
> ?lapply would get you started with R's Help, but that's rather dense.
>
> 4. You may also want to look at the plyr package which has organized
> and standardized in a convenient way much of the disparate
> functionality of the apply-type functions.
>
> 5. Finally, to answer your question, ?ave.
>
> Cheers,
> Bert
>
> On Tue, Sep 25, 2012 at 7:37 AM, Nico Met <nicomet80 at gmail.com> wrote:
>> Dear all,
>>
>> I have a data frame and I want to calculate mean of the first column which
>> is same name for example: CTK100 group
>>
>>> dput(test)
>> structure(list(Name = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 3L,
>> 3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
>> 2L), .Label = c("CTK100", "CTK103", "CTK121"), class = "factor"),
>>      Score = c(236.9726, 207.0055, 237.3464, 224.4774, 236.5034,
>>      206.7382, 233.94, 240.31, 240.9, 235.15, 223.36, 248.67,
>>      249.25, 201.4051, 244.1689, 182.2756, 229.001, 241.3211,
>>      196.0453, 232.6055, 225.0783, 196.0453, 232.6055, 225.0783
>>      )), .Names = c("Name", "Score"), class = "data.frame", row.names =
>> c(NA,
>> 24L))
>>
>>
>> How can write as a programme? because the original file has more than 60K
>> names with duplicates.
>>
>>
>> Many thanks
>>
>> Nico
>>
>>          [[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