title: Taking the sum in for loop in R
body: <p>This question may sound to be similar to other questions, but when I check them they are a bit different from what I'm looking for. Here's my code below,</p>
  
  <pre><code>Data &lt;- rbind(c(54, 55, 51), c(52, 51, 57))
  WMatrix &lt;- rbind(c(0, 1), c(1, 0)) 
  
  for(i in 1:2){
     for(j in 1:2){
        for(t in 1:2){
        print(WMatrix[i,j]*Data[j,t]*Data[i,t+1])
        }
     }
  }
  </code></pre>
  
  <p>The output of this codes is,</p>
  
  <pre><code>[1] 0
  [1] 0
  [1] 2860
  [1] 2601
  [1] 2754
  [1] 3135
  [1] 0
  [1] 0
  </code></pre>
  
  <p>Now, <strong><em>I want to have the sum of this output generated immediately from the loop (without listing it). That is, the output of the loop should be 11350, which is the sum of all generated outputs in the loop</em></strong>. I know this problem could be very simple, but I'm still learning basic programming. I tried this,</p>
  
  <pre><code>for(i in 1:2){
     for(j in 1:2){
        for(t in 1:2){
        print(sum(WMatrix[i,j]*Data[j,t]*Data[i,t+1]))
        }
     }
  }
  </code></pre>
  
  <p>But, the output is still the same, can someone help me on this?</p>
  
  <p>Thanks :)</p>