我想縮放矩陣中的值,以便每列加起來一個。我曾嘗試過:用colSums在R中劃分列
m = matrix(c(1:9),nrow=3, ncol=3, byrow=T)
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9
colSums(m)
12 15 18
m = m/colSums(m)
[,1] [,2] [,3]
[1,] 0.08333333 0.1666667 0.25
[2,] 0.26666667 0.3333333 0.40
[3,] 0.38888889 0.4444444 0.50
colSums(m)
[1] 0.7388889 0.9444444 1.1500000
很顯然這是行不通的。 然後我嘗試這樣做:
m = m/matrix(rep(colSums(m),3), nrow=3, ncol=3, byrow=T)
[,1] [,2] [,3]
[1,] 0.08333333 0.1333333 0.1666667
[2,] 0.33333333 0.3333333 0.3333333
[3,] 0.58333333 0.5333333 0.5000000
m = colSums(m)
[1] 1 1 1
所以這個工作,但感覺就像我失去了一些東西。這不可能是常規做法。我確定我在這裏很愚蠢。 任何幫助您能給將不勝感激 乾杯, 戴維
輝煌。謝謝!慚愧我完全忘了'prop.table()'。 – 2012-02-25 23:19:33