normalization - R plyr - Calculating scores based on grand and subject mean for each factor

I need to apply the following to a data.set which contains a number of aggregated scores.;

Dataset: P = Participant, TYPE = Trial type (factor), rt=score

`` TYPE P rt1 A 1 607.5002 A 2 481.0003 A 3 298.1254 A 5 568.250``

I need to calculate the following normalized score: NewScore = OldScore - Grandmean (mean of RT column) + Participant Mean (mean of RT column for the given subject, P)

I've been experimenting with ddply and have come up with the following;

``grandmean<-mean(data\$rt)ddply(data, .(P, TYPE), mutate, mean=mean(rt), grandmean=grandmean, subjectmean=mean(rt[P]), newscore=rt-grandmean-subjectmean)``

The key question here is; how do I get the subjectmean to subset the data based on the current rows subject.

Is ddply even appropriate here? I am trying to avoid using loops...

Thanks!

You didn't describe splitting on the `TYPE` column, so I'll leave it out here. But you're on the right track. I'd use `transform` though instead of `mutate`:

``````data\$grandmean <- mean(data\$rt)
ddply(data, .(P), transform, newscore = rt - grandmean - mean(rt))
``````

It is usually easiest to have `plyr` operate on a single thing, rather than trying to rely on it looking outside of its scope to find the global `grandmean`. So instead, make it a column.