[R] Getting group-wise standard scores of a vector
Matthew Dubins
matt.dubins at utoronto.ca
Wed Sep 26 22:56:38 CEST 2007
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
More information about the R-help
mailing list