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. 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? Thanks in advance! Sincerely, Tord Sn?ll ----------------------------------------------------------------------- Tord Sn?ll Avd. f v?xtekologi, Evolutionsbiologiskt centrum, Uppsala universitet Dept. of Plant Ecology, Evolutionary Biology Centre, Uppsala University Villav?gen 14 SE-752 36 Uppsala, Sweden Tel: 018-471 28 82 (int +46 18 471 28 82) (work) Tel: 018-25 71 33 (int +46 18 25 71 33) (home) Fax: 018-55 34 19 (int +46 18 55 34 19) (work) E-mail: Tord.Snall at ebc.uu.se http://www.vaxtbio.uu.se/resfold/tord_s.htm ------------------------------------------------------------------------ -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Dear Paulo and Martyn, Thanks a lot for this help. As a beginner I really appreciate the help available on this list. I searched for a suitable function in Venables & Ripley but only in the first two chapters. Now I know I can read about this kind of things in chapter 4. yours, Tord .At 15:19 2001-03-02 +0100, you wrote:>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 > >----------------------------------------------------------------------- Tord Sn?ll Avd. f v?xtekologi, Evolutionsbiologiskt centrum, Uppsala universitet Dept. of Plant Ecology, Evolutionary Biology Centre, Uppsala University Villav?gen 14 SE-752 36 Uppsala, Sweden Tel: 018-471 28 82 (int +46 18 471 28 82) (work) Tel: 018-25 71 33 (int +46 18 25 71 33) (home) Fax: 018-55 34 19 (int +46 18 55 34 19) (work) E-mail: Tord.Snall at ebc.uu.se http://www.vaxtbio.uu.se/resfold/tord_s.htm ------------------------------------------------------------------------ -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Hi Tord What about apply: apply(onorm, 2, function(x){(x - mean(x))/sqrt(var(x))}) Cheers P.J. On Fri, 2 Mar 2001, 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. > > 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? > > Thanks in advance! > > Sincerely, > Tord Sn?ll > > ----------------------------------------------------------------------- > Tord Sn?ll > Avd. f v?xtekologi, Evolutionsbiologiskt centrum, Uppsala universitet > Dept. of Plant Ecology, Evolutionary Biology Centre, Uppsala University > Villav?gen 14 > SE-752 36 Uppsala, Sweden > Tel: 018-471 28 82 (int +46 18 471 28 82) (work) > Tel: 018-25 71 33 (int +46 18 25 71 33) (home) > Fax: 018-55 34 19 (int +46 18 55 34 19) (work) > E-mail: Tord.Snall at ebc.uu.se > http://www.vaxtbio.uu.se/resfold/tord_s.htm > ------------------------------------------------------------------------ > > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- > 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 > _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._ >Paulo Justiniano Ribeiro Jr Dept Maths & Stats - Fylde College Lancaster University Lancaster LA1 4YF - U.K. e-mail: paulojus at est.ufpr.br http://www.maths.lancs.ac.uk/~ribeiro -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
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 at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
On Fri, 2 Mar 2001, 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.Hints (someone else will probably come along and give you the whole answer): see "sweep", "scale", "apply". For the names, names(newdata) <- paste("s",names(olddata),sep="")> > 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? > > Thanks in advance! > > Sincerely, > Tord Sn?ll > > ----------------------------------------------------------------------- > Tord Sn?ll > Avd. f v?xtekologi, Evolutionsbiologiskt centrum, Uppsala universitet > Dept. of Plant Ecology, Evolutionary Biology Centre, Uppsala University > Villav?gen 14 > SE-752 36 Uppsala, Sweden > Tel: 018-471 28 82 (int +46 18 471 28 82) (work) > Tel: 018-25 71 33 (int +46 18 25 71 33) (home) > Fax: 018-55 34 19 (int +46 18 55 34 19) (work) > E-mail: Tord.Snall at ebc.uu.se > http://www.vaxtbio.uu.se/resfold/tord_s.htm > ------------------------------------------------------------------------ > > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- > 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 > _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._ >-- 318 Carr Hall bolker at zoo.ufl.edu Zoology Department, University of Florida http://www.zoo.ufl.edu/bolker Box 118525 (ph) 352-392-5697 Gainesville, FL 32611-8525 (fax) 352-392-3704 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._