[R] Getting group-wise standard scores of a vector

Wayne.W.Jones at shell.com Wayne.W.Jones at shell.com
Thu Sep 27 08:49:49 CEST 2007


tapply is also very useful: 


my.df<-data.frame(x=rnorm(20, 50, 10),group=factor(sort(rep(c("A", "B"), 10))))
tapply(my.df$x,my.df$group,function(x){(x-mean(x))/sd(x)})



-----Original Message-----
From: r-help-bounces at r-project.org
[mailto:r-help-bounces at r-project.org]On Behalf Of Matthew Dubins
Sent: 26 September 2007 21:57
To: r-help at r-project.org
Subject: [R] Getting group-wise standard scores of a vector


Hi,

I want to be able to create a vector of z-scores from a vector of 
continuous data, conditional on a group membership vector.

Say you have 20 numbers distributed normally with a mean of 50 and an sd 
of 10:

x <- rnorm(20, 50, 10)


Then you have a vector that delineates 2 groups within x:

group <- sort(rep(c("A", "B"), 10))

test.data <- data.frame(cbind(x, group))

I know that if you break up the x vector into 2 different vectors then 
it becomes easy to calculate the z scores for each vector, then you 
stack them and append them to the original
data frame.  Is there anyway to apply this sort of calculation without 
splitting the original vector up?  I tried a really complex ifelse 
statement but it didn't seem to work.

Thanks in advance,
Matthew Dubins

______________________________________________
R-help at 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.



More information about the R-help mailing list