[R] Re: z-scores for different factor levels

Stuart Leask stuart.leask at nottingham.ac.uk
Fri Jan 18 16:09:22 CET 2002


Thanks, Greg.

I had a feeling there must be a few different solutions to this (pretty
common) problem.

Stuart

----- Original Message -----
From: Warnes, Gregory R <gregory_r_warnes at groton.pfizer.com>
To: 'Stuart Leask' <stuart.leask at nottingham.ac.uk>
Cc: <r-help at stat.math.ethz.ch>
Sent: Friday, January 18, 2002 2:37 PM
Subject: RE: z-scores for different factor levels


> Hi Stuart,
>
> I often use this small function
>
> standardize <- function(x)  ( x - mean(x, na.rm=T) ) / sqrt(var(x,
> na.rm=T))
>
> to standardize variables.
>
> You should be able to use this to do what you want by splitting the data
> frame into sections based on the factor level, using standardize() to
create
> a new variable in each section, then paste the data frame back together.
>
> Something like:
>
> # create test data
> test <- data.frame( f=sample(c("A","B","C"),100,replace=T),
>                            x1=rnorm(100,2,5), x2=rnorm(100) )
> # split by factor
> subsets <- split(test,test$f)
>
> # create the standardized version of x1
> subsets <- lapply(subsets, function(s) { s$x1s <- standardize(s$x1);
> s} )
>
> # paste the results back together
> result <- data.frame()
> for(s in subsets) result <- rbind(result, s)
>
> -Greg
>
> -----Original Message-----
> From: Stuart Leask [mailto:stuart.leask at nottingham.ac.uk]
> Sent: Friday, January 18, 2002 5:33 AM
> To: r-help at stat.math.ethz.ch
> Subject: [R]: z-scores for different factor levels
>
>
> Hi there.
> I am trying to generate z-scores for a variable according to it's factor
> level, leaving this result in the original dataframe.
>
> ie. standardised birth weight for gestational age in weeks.
> BWT is birthweight
> GEST is gestational age in weeks (equivalent to the factor level)
>
> I can generate the factor level mean & SD using tapply
> tapply(BWT,GEST,mean) etc.
> but this creates a new array of means & SDs.
>
> - Can anyone suggest how I can slot these means & SDs by factor level
> straight back into the original dataframe, so I can then subtract the mean
&
> divide by the SD to get a Z-score for each case?
> - Is there a function already available that can generate z-scores by
factor
> levels?
>
> Stuart
>
> Dr Stuart Leask MA MRCPsych, Clinical Lecturer in Psychiatry
> University of Nottingham Dept of Psychiatry, Duncan Macmillan House
> Porchester Road, Nottingham. NG3 6AA. UK
> http://www.nottingham.ac.uk/psychiatry/staff/sjl.html
>
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.
-.
> -.-
> r-help mailing list -- Read
http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
> Send "info", "help", or "[un]subscribe"
> (in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
>
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._.
> _._
>
>
> LEGAL NOTICE
> Unless expressly stated otherwise, this message is confidential and may be
privileged. It is intended for the addressee(s) only. Access to this E-mail
by anyone else is unauthorized. If you are not an addressee, any disclosure
or copying of the contents of this E-mail or any action taken (or not taken)
in reliance on it is unauthorized and may be unlawful. If you are not an
addressee, please inform the sender immediately.

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list