Is there a way to increment particular rows and columns of a matrix using indexing of that matrix and in the same time do it twice if the indices repeat. For example, with the following code:
rows <- c(1, 2, 3, 4, 1)
cols <- c(4, 2, 5, 1, 4)
freq <- matrix(0, 5, 5)
freq[cbind(rows, cols)] <- freq[cbind(rows, cols)] + 1
freq
I get ones on the particular rows and columns, but since the (1, 4) combination is repeated twice, I would like to find a way to add another 1 to it and make it two in a computationally efficient way using R's indexing of matrix like above.
Thanks in advance!