Re: [R] Help on aggregate method

From: Stella Pachidi <stella.pachidi_at_gmail.com>
Date: Tue, 01 Jun 2010 18:19:07 +0200

Dear Erik,

Thank you very much. Indeed ave did the same job amazingly fast! I did not know the function before.

Many thanks to all R experts who answer to this mailing list, it's amazing how much help you offer to the newbies :)

Kind regards,
Stella

On Tue, Jun 1, 2010 at 6:11 PM, Erik Iverson <eriki_at_ccbr.umn.edu> wrote:

>
>
> Stella Pachidi wrote:
>
>> Dear Erik and R experts,
>>
>> Thank you for the fast response!
>>
>> I include an example with the ChickWeight dataset:
>>
>> ap.dat <- ChickWeight
>>
>> matchMeanEx <- function(ind,dataTable,aggrTable)
>> {
>> index <- which((aggrTable[,1]==dataTable[["Diet"]][ind]) &
>> (aggrTable[,2]==dataTable[["Chick"]][ind]))
>> as.numeric(aggrTable[index,3])
>> }
>>
>> avgW <- aggregate(ap.dat[["weight"]], by = list(ap.dat[["Diet"]],
>> ap.dat[["Chick"]]), FUN="mean")
>> meanW <- sapply((1:length(ap.dat[,1])), FUN=matchMeanEx, ap.dat, avgW)
>> ap.dat <- cbind (ap.dat, meanW)
>>
>>
>
> How about simply using ave.
>
> ap.dat$meanW <- ave(ap.dat$weight, list(ap.dat$Diet, ap.dat$Chick))
>

-- 
Stella Pachidi
Master in Business Informatics student
Utrecht University
email: S.Pachidi_at_students.uu.nl
tel: +31644478898

	[[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.
Received on Tue 01 Jun 2010 - 16:21:37 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 01 Jun 2010 - 16:40:25 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive