[R] scale function within a for-loop?
dimitris.rizopoulos at med.kuleuven.be
Tue Sep 20 10:57:04 CEST 2005
you are right scale() will produce the standardized version of the
matrix as documented; a note though, for columns means and standard
deviations it's better to use colMeans() and sd() directly instead of
apply(mat, 2, mean) and apply(mat, 2, sd), i.e.,
sc.mat <- scale(as.matrix(kalcin[, 3:7]))
School of Public Health
Catholic University of Leuven
Address: Kapucijnenvoer 35, Leuven, Belgium
----- Original Message -----
From: "Petr Pikal" <petr.pikal at precheza.cz>
To: "Frank Schmid" <frank.schmid at vwi.unibe.ch>;
<r-help at stat.math.ethz.ch>
Sent: Tuesday, September 20, 2005 10:39 AM
Subject: Re: [R] scale function within a for-loop?
> I maybe mistaken but
> 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
> On 20 Sep 2005 at 9:13, Frank Schmid wrote:
>> I have a data matrix containing around 180 variables and more than
>> 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
>> 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
>> 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
>> way that gives the same result without using a for-loop?
>> [[alternative HTML version deleted]]
>> R-help at stat.math.ethz.ch mailing list
>> PLEASE do read the posting guide!
> Petr Pikal
> petr.pikal at precheza.cz
> R-help at stat.math.ethz.ch mailing list
> PLEASE do read the posting guide!
More information about the R-help