title: R Studio: Average multiple columns with conditional logic
body: <p>In R studio, how can I get one average of multiple columns with conditional logic for one of the columns? For example, I have a dataset, called 'test' of the below 5 columns. I want to get one combined average for columns A, B, and C. But I only want to include Column C in the average if Flag is equal to 1 for a cell in Column C. </p>
  
  <pre><code>    ID&lt;-c(1,2,3)
      A&lt;-c(1,0,NA)
      B&lt;-c(0,NA,1)
      C&lt;-c(1,1,1)
      Flag&lt;-c(0,1,1)
     test&lt;-as.data.frame(cbind(ID,A,B,C,Flag))
  </code></pre>
  
  <p><a href="http://i.stack.imgur.com/Vqs3V.png" rel="nofollow"> [click here to see my data set]</a></p>
  
  <p>So I want: Average = (All cells in column A + All cells in column B + Only cells in column C where cells in Flag column is equal to 1 )</p>