[R] How to z-standardize for subgroups?

John Kane jrkrideau at yahoo.ca
Sun Nov 29 22:23:12 CET 2009


http://finzi.psych.upenn.edu/R/library/QuantPsyc/html/Make.Z.html

Make.Z in the QuantPsych package may already do it.

--- On Sun, 11/29/09, Karsten Wolf <wolf at uni-bremen.de> wrote:

> From: Karsten Wolf <wolf at uni-bremen.de>
> Subject: [R] How to z-standardize for subgroups?
> To: r-help at r-project.org
> Received: Sunday, November 29, 2009, 10:41 AM
> Hi folks,
> I have a dataframe df.vars with the follwing structure:
> 
> 
> var1   var2   var3   group
> 
> Group is a factor.
> 
> Now I want to standardize the vars 1-3 (actually - there
> are many more) by class, so I define
> 
> z.mean.sd <- function(data){
>     return.values <- (data  -
> mean(data)) / (sd(data))
>     return(return.values)
> }
> 
> now I can call for each var
> 
> z.var1 <- by(df.vars$var1, group, z.mean.sd)
> 
> which gives me the standardised data for each subgroup in a
> list with the subgroups
> 
> z.var1 <- unlist(z.var1)
> 
> then gives me the z-standardised data for var1 in one
> vector. Great!
> 
> Now I would like to do this for the whole dataframe, but
> probably I am not thinking vectorwise enough.
> 
> z.df.vars <- by(df.vars, group, z.mean.sd)
> 
> does not work. I banged my head on other solutions trying
> out sapply and tapply, but did not succeed. Do I need to
> loop and put everything together by hand? But I want to keep
> the columnnames in the vector…
> 
> -karsten
> 
> 
> ---------------------------------------------------------------------------------------------
> Karsten D. Wolf
> Didactical Design of Interactive
> Learning Environments
> Universität Bremen - Fachbereich 12
> web: http://www.ifeb.uni-bremen.de/wolf/
> 
> ______________________________________________
> 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.
> 

__________________________________________________
Do You Yahoo!?
Tired of spam?



More information about the R-help mailing list