[R] Scaling error

jim holtman
Sun, 27 Dec 2009 23:38:05 -0000

sweep(x, 2, m, FUN='/')

On Sun, Dec 27, 2009 at 2:26 PM, Muhammad Rahiz <
muhammad.rahiz@ouce.ox.ac.uk> wrote:

> 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@ouce.ox.ac.uk
>
>
> ______________________________________________
> R-help@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<http://www.r-project.org/posting-guide.html>
> and provide commented, minimal, self-contained, reproducible code.
>



--
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?

	[[alternative HTML version deleted]]