RE: [R] standardising a data matrix

About this list Date view Thread view Subject view Author view

From: Martyn Plummer (plummer@iarc.fr)
Date: Sat 03 Mar 2001 - 00:19:28 EST


Message-ID: <XFMail.010302151928.plummer@iarc.fr>

On 02-Mar-01 Tord Snall wrote:
> Dear all,
> I would like to standardise my data. Here is an exemple for two columns:
>
>> onorm<- read.table("onorm.txt", header=T)
>> names(onorm)
> [1] "on1" "on2" "on3" "on4" "on5"
>> norm<- data.frame((onorm$on1-mean(onorm$on1))/stdev(onorm$on1),
> (onorm$on2-mean(onorm$on2))/stdev(onorm$on2))
>
> I would be happy however not having got to do this coulumnwise since I have
> about 80 columns. Could someone please give a hint on how to do this for
> the whole matrix.

Use sweep()

norm <- sweep(onorm, 2, apply(onorm, 2, mean), "-") # standardize mean to 0
norm <- sweep(norm, 2, apply(norm, 2, sd), "/") # standardize variance to 1

> The names of the coulumns are on1, on2 etc. as above. I want the columns in
> the new standardised dataset to have the names son1, son2 etc, i.e. adding
> an s to all the original column names. How do I do that?

names(norm) <- paste("s", names(norm), sep="")

Martyn

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._


About this list Date view Thread view Subject view Author view

This archive was generated by hypermail 2b30 : Fri 22 Jun 2001 - 18:58:34 EST