当前位置: 动力学知识库 > 问答 > 编程问答 >

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 rt

1 A 1 607.500

2 A 2 481.000

3 A 3 298.125

4 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.

分享给朋友:
您可能感兴趣的文章:
随机阅读: