title: Summing values in R based on column value with dplyr
body: <p>I have a data set that has the following information:</p>
  
  <pre><code>Subject    Value1    Value2    Value3      UniqueNumber
  001        1         0         1           3
  002        0         1         1           2
  003        1         1         1           1
  </code></pre>
  
  <p>If the value of UniqueNumber > 0, I would like to sum the values with dplyr for each subject from rows 1 through UniqueNumber and calculate the mean. So for Subject 001, sum = 2 and mean = .67.</p>
  
  <pre><code>total = 0;
  average = 0;
  for(i in 1:length(Data$Subject)){
     for(j in 1:ncols(Data)){
     if(Data$UniqueNumber[i] &gt; 0){
      total[i] = sum(Data[i,1:j])
      average[i] = mean(Data[i,1:j])
     }
  }
  </code></pre>
  
  <p>Edit: I am only looking to sum through the number of columns listed in the 'UniqueNumber' column. So this is looping through every row and stopping at column listed in 'UniqueNumber'. 
  Example: Row 2 with Subject 002 should sum up the values in columns 'Value1' and 'Value2', while Row 3 with Subject 003 should only sum the value in column 'Value1'. </p>