[R] simple matrix division.

Gabor Grothendieck ggrothendieck at gmail.com
Sat Sep 2 21:16:56 CEST 2006


Try:

prop.table(x, 2)


On 9/2/06, Alexander Nervedi <alexnerdy at hotmail.com> wrote:
> Hi
>
> I have
>
> >x<-matrix(c(1,2,3,4), ncol = 2)
> >x
>     [,1] [,2]
> [1,]    1    3
> [2,]    2    4
>
> I'd like these two be divided by their column totals. so 1/3 and 2/3 and 3/7
> and 4/7. The obvious
> >x/colSums(x)
>          [,1]      [,2]
> [1,] 0.3333333 1.0000000
> [2,] 0.2857143 0.5714286
>
> gives me the wrong results for off diagnal ones,  since it divides the first
> row by 3 and second by 7. The inelegant
> >t(t(x)/colSums(x))
>          [,1]      [,2]
> [1,] 0.3333333 0.4285714
> [2,] 0.6666667 0.5714286
>
> gives me the right thing. I was wondering if there is any better way of
> getting what I want.
>
> thanks
>
> Alex
>
> _________________________________________________________________
> Get real-time traffic reports with Windows Live Local Search
>
> ______________________________________________
> 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
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list