[R] how to divide each column in a matrix by its colSums?
Spencer Graves
spencer.graves at structuremonitoring.com
Sun Nov 28 22:44:12 CET 2010
prop.table divides every element by the matrix total, not its colSums:
> m <- matrix(1:4, 2)
> m
[,1] [,2]
[1,] 1 3
[2,] 2 4
> prop.table(m)
[,1] [,2]
[1,] 0.1 0.3
[2,] 0.2 0.4
m/rowSums(m) divides every row by its rowSum:
> m/rowSums(m)
[,1] [,2]
[1,] 0.2500000 0.7500000
[2,] 0.3333333 0.6666667
To divide every element by its colSum:
> t(t(m)/rowSums(t(m)))
[,1] [,2]
[1,] 0.3333333 0.4285714
[2,] 0.6666667 0.5714286
Hope this helps.
Spencer
On 11/28/2010 12:55 PM, casperyc wrote:
> In that case, there are values>1,
> which is clearly not what I wanted.
>
> Thanks.
>
> I think I should use prop.table
More information about the R-help
mailing list