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
.
One option is tapply
from base R
where the grouping is based on 'row_index'
unname(tapply(array, row_index, FUN = sum))
#[1] 6.8 7.5 11.0 3.1
Or another is by
by(array, row_index, FUN = sum)
Or with rowsum
unname(rowsum(array, row_index)[,1])
#[1] 6.8 7.5 11.0 3.1
Or with xtabs
xtabs(array ~ row_index)
NOTE: All of the above solutions are from base R