[R] scale function within a for-loop?

Dimitris Rizopoulos dimitris.rizopoulos at med.kuleuven.be
Tue Sep 20 10:57:04 CEST 2005


Hi Petr,

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]))

colMeans(sc.mat)
sd(sc.mat)


Best,
Dimitris

----
Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/16/336899
Fax: +32/16/337015
Web: http://www.med.kuleuven.be/biostat/
     http://www.student.kuleuven.be/~m0390867/dimitris.htm


----- 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?


> 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
>
> ______________________________________________
> 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
> 


Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm




More information about the R-help mailing list