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

Armin Raznahan raznahana at mail.nih.gov
Mon May 18 23:43:33 CEST 2009


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

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

Yours hopefully


More information about the R-help mailing list