[R] Scaling error

baptiste auguie baptiste.auguie at googlemail.com
Sun Dec 27 21:28:26 CET 2009


Hi,

Try this,

x <- matrix(1:9, ncol=3, byrow=T)

sca <- c(2.5, 1.7, 3.6)

x %*% diag(1/sca)


HTH,

baptiste

2009/12/27 Muhammad Rahiz <muhammad.rahiz at ouce.ox.ac.uk>:
> 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