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)


Best regards,
Stella


On Tue, Jun 1, 2010 at 4:58 PM, Erik Iverson <eriki@ccbr.umn.edu> wrote:

> It's easiest for us to help if you give us a reproducible example.  We
> don't have your datasets (ap.dat), so we can't run your code below. It's
> easy to create sample data with the random number generators in R, or use
> ?dput to give us a sample of your actual data.frame.
>
> I would guess your problem is solved by ?ave though.
>
> Stella Pachidi wrote:
>
>> Dear R experts,
>>
>> I would really appreciate if you had an idea on how to use more
>> efficiently the aggregate method:
>>
>> More specifically, I would like to calculate the mean of certain
>> values on a data frame,  grouped by various attributes, and then
>> create a new column in the data frame that will have the corresponding
>> mean for every row. I attach part of my code:
>>
>> matchMean <- function(ind,dataTable,aggrTable)
>> {
>>    index <- which((aggrTable[,1]==dataTable[["Attr1"]][ind]) &
>> (aggrTable[,2]==dataTable[["Attr2"]][ind]))
>>    as.numeric(aggrTable[index,3])
>> }
>>
>> avgDur <- aggregate(ap.dat[["Dur"]], by = list(ap.dat[["Attr1"]],
>> ap.dat[["Attr2"]]), FUN="mean")
>> meanDur <- sapply((1:length(ap.dat[,1])), FUN=matchMean, ap.dat, avgDur)
>> ap.dat <- cbind (ap.dat, meanDur)
>>
>> As I deal with very large dataset, it takes long time to run my
>> matching function, so if you had an idea on how to automate more this
>> matching process I would be really grateful.
>>
>> Thank you very much in advance!
>>
>> Kind regards,
>> Stella
>>
>>
>>
>> --
>> Stella Pachidi
>> Master in Business Informatics student
>> Utrecht University
>>
>> ______________________________________________
>> R-help@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.
>>
>


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

	[[alternative HTML version deleted]]

