[R] Scaling error

Daniel Malter daniel at umd.edu
Sun Dec 27 20:41:59 CET 2009


If  "out"  is what you want to achieve, why don't you multiply "sca" (called
"m" below) with the transpose of "x" and then transpose the resulting
matrix?

x=c(1,2,3,4,5,6,7,8,9)
dim(x)=c(3,3)
x=t(x)

x
m=c(2.5,1.7,3.6)

x*m #returns what you don't want

t(t(x)*m) #returns what you want

HTH
Daniel

-------------------------
cuncta stricte discussurus
-------------------------
-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
Behalf Of Muhammad Rahiz
Sent: Sunday, December 27, 2009 2:27 PM
To: r-help at r-project.org
Subject: [R] Scaling error

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

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list