Sid0311
Sid0311

Reputation: 111

Calculate z-score by reference group

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

Answers (1)

emilliman5
emilliman5

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

Related Questions