[R] scale function within a for-loop?

Petr Pikal petr.pikal at precheza.cz
Tue Sep 20 10:39:22 CEST 2005


Hi

I maybe mistaken but

scale(your.matrix)

gives you matrix scaled in the way you want.

> apply(scale(as.matrix(kalcin[,3:7])), na.rm=T,2,sd)
vodofe  stroz      l      a      b 
     1      1      1      1      1 
> apply(scale(as.matrix(kalcin[,3:7])), na.rm=T,2,mean)
       vodofe         stroz             l             a             b 

 1.990322e-15 -5.025086e-14  8.581765e-14  3.588313e-15 -1.370877e-15 

HTH
Petr


On 20 Sep 2005 at 9:13, Frank Schmid wrote:

> I have a data matrix containing around 180 variables and more than 470
> observations for each and no missing values. 
> 
> Within a for-loop, the first step of calculations is to standardize
> each column, such that the mean of each column is zero and the sd is
> one. The for-loop starts with a subset of the initial matrix and
> includes all columns but only a third of the rows. The loop "works
> itself through" the whole matrix and adds everytimes one row, so in
> the last loop, the whole data matrix is used. The standardization
> within the loop is done using the "scale" function. 
> 
> Now, my problem is that with all the 180 variables, either the
> for-loop or the scale function does not work properly, as the
> resulting matrix after the standardization does not have the same
> dimensions anymore as it had before. The matrix is no longer a 180*470
> matrix, but a 180*130 matrix. If, however, I include only 130
> variables instead of 180, the result is correct, the dimensions are
> right and each column indeed has mean zero and sd one. 
> 
> Can anyone please tell me, why this problem appears? Would there be a
> way that gives the same result without using a for-loop?
> 
> Thanks
> 
> 
> 
> 
> 
>  [[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html

Petr Pikal
petr.pikal at precheza.cz




More information about the R-help mailing list