Skip to content
Prev 202067 / 398503 Next

How to z-standardize for subgroups?

Hi Jorge, Chuck and Kane,
thanks for your input!
The following code based on Jorge's answer did the trick to  
standardize for subgroups within multiple columns:

# define a standardize function, but you could also define your custom  
standardize function here
z.mean.sd <- function(data){
	return.values <- (data  - mean(data, na.rm = TRUE)) / (sd(data, na.rm  
= TRUE))
	return(return.values)
}

# assume there is some data.frame called sole.data with a group factor  
sole.data$studie already read into R
sole.data <- read.csv2("SoLe.dat")
attach(sole.data)
# assume I have created a subset of the data.frame cor.vars with only  
some of the vars needed to be standardized
cor.vars <- data.frame(var02, var04, var07, var10, var17, var24, var 36)

z.cor.vars <- apply(cor.vars, 2, tapply, sole.data$studie, z.mean.sd)
z.cor.vars <- sapply(z.cor.vars, unlist, USE.NAMES = FALSE)
z.cor.vars

BUT then Chuck's answer was much more elegant than my first woodpecker  
solution

apply(iris[,1:4], 2, function(x){ave(x, iris$Species, FUN = scale)})

could be translated into

apply(sole.data[,c(2,4,7,10,17,24,36)], 2, function(x){ave(x,sole.data 
$studie, FUN=scale)})

Thanks for the beauty of this code with an anonymous function call :)

-karsten



Am 29.11.2009 um 16:47 schrieb Jorge Ivan Velez: