Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 204824

how to avoid for loop using apply function in this question

$
0
0
mat<-matrix(1:9,nrow=3,ncol=3)
for(i in 1:3){
  print(colSums(mat[1:i,]))
}

I'm trying to calculate mean of colSums of part of a matrix. enter image description here How do I avoid for loop in this case? The answer may be similar to the code below but I don't know how to proceed.

apply(mat,2,function(x) colSums(mat[]))

Thanks in advance!


Viewing all articles
Browse latest Browse all 204824

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>