I'm having issues in doing Excel's SUMIF function in R. There are 2 matrices, m and n. I want matrix n to take the sum of each column j of matrix m limited until the i-th row if the row i+1 in column j+1 is not empty (not sure if I made this clear, below are my explanation for a clearer view of what I want to do).

Below are my codes:

```
m <- matrix(c(1,2,3,4,5,'',7,'',''), nrow = 3)

n <- matrix('', nrow = 2, ncol = 3)

for (j in 1:2) {
  n[2,j] <- sum(as.numeric(m[,j])[!is.na(m[,j+1])]
}

n[2,3] <- ''

```

Below is matrix m:

```
> m <- matrix(c(1,2,3,4,5,'',7,'',''), nrow = 3)
> m
     [,1] [,2] [,3]
[1,] "1"  "4"  "7" 
[2,] "2"  "5"  ""  
[3,] "3"  ""   ""  

```

The above codes yield the results for matrix n:

```
> n <- matrix('', nrow = 2, ncol = 3)
> n
     [,1] [,2] [,3]
[1,] ""   ""   ""  
[2,] ""   ""   ""  

```

But I want the codes to yield this results:

```
> n <- matrix('', nrow = 2, ncol = 3)
> n
     [,1] [,2] [,3]
[1,] ""   ""   ""  
[2,] "3"  "4"  ""  

```

Please help! Thanks!