[R] z-transform each column of a data.frame

Steve Lianoglou mailinglist.honeypot at gmail.com
Fri Jan 20 18:12:52 CET 2012


Hi Martin,

On Fri, Jan 20, 2012 at 12:04 PM, Martin Batholdy
<batholdy at googlemail.com> wrote:
> Hi,
>
>
> I am currently trying to z-transform (that is subtracting the mean and divide by the standard deviation) multiple columns of a data.frame at the same time.
>
>
> My first approach was:
>
> x <- data.frame(c(0:10), c(10:20))
> (x - colMeans(x)) / apply(x, 2, sd)
>
>
> This is obviously not working.
>
> Is there a convenient way to z-transform each column separately (so in this case, each column represents an independent variable that should be z-transformed)

The `scale` function essentially does this except it divides by the
sample standard deviation.

Punch `scale.default` into your R session to see the code if you want
to see how one way you could write the code yourself.

HTH,
-steve

-- 
Steve Lianoglou
Graduate Student: Computational Systems Biology
 | Memorial Sloan-Kettering Cancer Center
 | Weill Medical College of Cornell University
Contact Info: http://cbio.mskcc.org/~lianos/contact



More information about the R-help mailing list