Reputation: 111
data<-data.frame(ID=rep(1:12), group=rep(1:3, times=4), value=(rnorm(12, 5:15)))
How do I calculate z-scores for groups 2 and 3 values, in relation to the mean and SD of group 1?
Surely there is an easy way, but I don't seem to get how to do it!
Upvotes: 2
Views: 880
Reputation: 5956
I think this is what you want. Nothing special needed, we just make use of R's vectorized operations.
data$zScore<-(data$value-mean(data$value[data$group==1]))/sd(data$value[data$group==1])
Upvotes: 2