I have an array in which I need to add elements together based on the index in which they belong.
row_index <- c(1, 3, 1, 2, 2, 3, 3, 4)
array <- c(1.2, 3.4, 5.6, 7.2, 0.3, 5.6, 2.0, 3.1)
So in this case I want to add all elements that belong on the first row according to row_index
together. For example, 1.2
and 5.6
would be added together since they correspond to the same row based on row_index
and similarly for row 2 it would be 7.2 + 0.3
. So my resulting array would be.
result = (6.8 , 7.5 , 11.0 , 3.1)
I have no clue how to do this in R. For context, I have a sparse matrix and accessing which row each non-zero elements belongs to is very easy and efficient to do, and the length of row_index
will always be the same as array
.