colsum rowsum populating matrix

apply, if-statement, matrix, r

Solution

This can be thought of as an outer product of the row and column sums, where the function takes the minimum value:

outer(rowSums(x), colSums(x), FUN=pmin)
##      [,1] [,2] [,3]
## [1,]    3    7    9
## [2,]    3    7   11

Problem

I'm trying to write for each cell entry in a matrix what value is smallest, either its rowsum value or colsum value in a new matrix of the same dimension. For example: say I have matrix c which looks like this: ``` x <- matrix(seq(1:6),2) x [,1] [,2] [,3] [1,] 1 3 5 [2,] 2 4 6 ``` Its rowsum and colsum are: ``` rowSums(x) [1] 9 12 colSums(x) [1] 3 7 11 ``` so based on that info, the new matrix should look like this: ``` [,1] [,2] [,3] [1,] 3 7 9 [2,] 3 7 11 ``` I've been thinking about using apply but I do not know how I can write an if statement to write the smallest value from either rowsum or colsum for each cell entry. Any ideas?

Original source