I have a matrix A that has a large number of rows and columns (below one example of such a matrix) that occasionally has a full row of 0 values (as in row 4 at this particular example).
I want to have a function that checks all rows of A and allows me to perform an operation on each element of these rows. Is there an easy way to do that?
I also wonder if matrix is the right data structure for this. It feels not quite right, perhaps data frames are better for that?
A = matrix(
c(0, 0, 1, 0, 0, 0, 0,
1, 0, 1, 1, 0, 0, 0,
0, 0, 0, 1, 1, 0, 0,
0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 1, 1,
0, 0, 0, 1, 1, 0, 1), nrow=7,ncol=7,byrow = TRUE)
For every row of that matrix I want to determine if there are only 0's in it. If so, I want to set (for each element) the value 1/N (where N is the ncol(A)).
Sudo code:
If (sum(row of A) == 0) then row_of_A = 1/ncol(A)
applyto loop over matrix rows, but there may be a more efficient alternative. - Roland