[R] Using apply() and scale() in combination

Peter Alspach PAlspach at hortresearch.co.nz
Tue May 19 02:25:04 CEST 2009


Tena koe Armin

From the help file of apply():

     If each call to 'FUN' returns a vector of length 'n', then 'apply'
     returns an array of dimension 'c(n, dim(X)[MARGIN])' if 'n > 1'. 
  
So, one should expect what you got in the second case.  You can, of
course, transpose the result using t().

HTH .....

Peter Alspach

> -----Original Message-----
> From: r-help-bounces at r-project.org 
> [mailto:r-help-bounces at r-project.org] On Behalf Of Armin Raznahan
> Sent: Tuesday, 19 May 2009 9:44 a.m.
> To: r-help at r-project.org
> Subject: [R] Using apply() and scale() in combination
> 
> Hello
> 
> I am an R newbie, and am coming against a couple of problems 
> when I try and apply the scale function across all the rows 
> of a  matrix.
> 
> -------------------------
> #I have a matrix "dt_l".
> 
> > str(dt_l)
>  num [1:40962, 1:885] 3.04 4.1 3.4 3.58 3.77 ...
> 
> #I want to convert the values in each row of this matrix into 
> standardised values (mean 0, sd 1). I believe the command for 
> doing this is "scale()", with the defaults for this function 
> being to set mean at 0 and sd at 1.
> Therefore, my desired end-point is an object with the same 
> dimensions as dt_l.
> 
> So, I use this command..which I think should applying scale 
> to every row of matrix dt_l:
> 
> > dt.stand<-apply(dt_l, 1, scale)
> 
> #This has led to two different sorts of problem:
> 
> (1) On some occasions when I have run the command I get an 
> error warning which includes the phrase
> 
> "error: can't allocate region..."
> 
> (2) On other occasions (when as far as I know I have entered 
> the same command), it runs. BUT, when I look at the object 
> produced the dimensions are different from dt_l ....
> 
> > str(dt.stand_l)
>  num [1:885, 1:40962] -0.679 -0.0882 0.1204 1.8571 0.8854 ...
> 
> ----------------------------
> 
> I would be very grateful for any explanations/fixes that can 
> be offered for the above, or any suggestions of alternative 
> ways of reaching my desired end-point.
> 
> Yours hopefully
> 
> Armin
> 
> ______________________________________________
> 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.
> 

The contents of this e-mail are confidential and may be subject to legal privilege.
 If you are not the intended recipient you must not use, disseminate, distribute or
 reproduce all or any part of this e-mail or attachments.  If you have received this
 e-mail in error, please notify the sender and delete all material pertaining to this
 e-mail.  Any opinion or views expressed in this e-mail are those of the individual
 sender and may not represent those of The New Zealand Institute for Plant and
 Food Research Limited.




More information about the R-help mailing list