0

I have a matrix of 2 columns. I calculated the percentage contribution of each column by row to the overall using the formula below:

B = matrix (c(2,4,3,1,5,7,5,3,8,3,7,3),nrow=6,ncol=2) 
sweep(B, 1, rowSums(B), FUN="/")

This gives the result below:

          [,1]      [,2]
[1,] 0.2857143 0.7142857
[2,] 0.5714286 0.4285714
[3,] 0.2727273 0.7272727
[4,] 0.2500000 0.7500000
[5,] 0.4166667 0.5833333
[6,] 0.7000000 0.3000000

However, I need to add weights to each row i.e. 1 for row 1 and 3 for row 2. How do I compute the weighted percentage contribution, please?

Joke O.
  • 515
  • 6
  • 29
  • `I need to add weights to each row` What weights? You only mention row 1 & 2. What is the expected output? – mtoto Mar 18 '16 at 14:04
  • I added "1 and 3" i.e. If I had 10 and 4 and wanted to add these weights, it will be ((10*1) + (4*3))/4 which will be 4. The weights I want to add are "1" for the first row and "2" for the second row. – Joke O. Mar 18 '16 at 14:06
  • 3
    Unclear, please provide expected output. – mtoto Mar 18 '16 at 14:08

0 Answers0