[R] Scaling error

Muhammad Rahiz muhammad.rahiz at ouce.ox.ac.uk
Sun Dec 27 20:26:39 CET 2009


Hi useRs,

I ran into an inconsistent output problem again. Here is the simplify 
illustration

I've got a matrix as follows

 > x
        V1    V2   V3
[1,]   1      2     3
[2,]   4      5     6
[3,]   7      8     9

Associated with the matrix is a scaling factor, sca, derived from, say 
the mean of the respective columns, V1-V3;

 > sca
        V1    V2   V3
        2.5   1.7   3.6

The idea is that the scaling factor gets applied to each element in the 
column matrix. So

 > out <- x / sca

would give me

V1            V2             V3
1 *2.5       2 *1.7       3 *3.6
4 *2.5       5 *1.7       6 *3.6
7 *2.5       8 *1.7       9 *3.6

But what actually happen is this,

V1            V2             V3
1 *2.5       2 *2.5       3 *2.5
4 *1.7       5 *1.7       6 *1.7
7 *3.6       8 *3.6       9 *3.6

I can do the following;

< x[,1] / sca[1]
< x[,2] / sca[2]

which is OK for a set of test data but not for my actual dataset.

At the moment, I'm thinking of something in the lines of a for loop 
function i.e.

for (i in ...){
statement...
}

Is there a syntax in the for loop that allows me to select the 
column/row in the file?

Thanks.

-- 
Muhammad Rahiz  |  Doctoral Student in Regional Climate Modeling					
Climate Research Laboratory, School of Geography & the Environment	
Oxford University Centre for the Environment
South Parks Road, Oxford, OX1 3QY, United Kingdom 
Tel: +44 (0)1865-285194	 Mobile: +44 (0)7854-625974
Email: muhammad.rahiz at ouce.ox.ac.uk




More information about the R-help mailing list