title: How to create a new column in a data frame depending on multiple criteria from multiple columns from the same data frame
body: <p>I have a data frame <code>df1</code> with four variables. One refers to sunlight, the second one refers to the moon-phase light (light due to the moon's phase), the third one to the moon-position light (light from the moon depending on if it is in the sky or not) and the fourth refers to the clarity of the sky (opposite to cloudiness). </p>
  
  <p>I call them <code>SL</code>, <code>MPhL</code>, <code>MPL</code> and <code>SC</code> respectively. I want to create a new column referred to "global light" that during the day depends only on <code>SL</code> and during the night depends on the other three columns ("MPhL", "MPL" and "SC"). What I want is that at night (when <code>SL == 0</code>), the light in a specific area is equal to the product of the columns "MPhL", "MPL" and "SC". If any of them is 0, then, the light at night would be 0 also.</p>
  
  <p>Since I work with a matrix of hundreds of thousands of rows, what would be the best way to do it? As an example of what I have:</p>
  
  <pre><code>SL&lt;- c(0.82,0.00,0.24,0.00,0.98,0.24,0.00,0.00)
  MPhL&lt;- c(0.95,0.85,0.65,0.35,0.15,0.00,0.87,0.74)
  MPL&lt;- c(0.00,0.50,0.10,0.89,0.33,0.58,0.00,0.46)
  SC&lt;- c(0.00,0.50,0.10,0.89,0.33,0.58,0.00,0.46)
  df&lt;-data.frame(SL,MPhL,MPL,SC)
  df
      SL MPhL  MPL   SC
  1 0.82 0.95 0.00 0.00
  2 0.00 0.85 0.50 0.50
  3 0.24 0.65 0.10 0.10
  4 0.00 0.35 0.89 0.89
  5 0.98 0.15 0.33 0.33
  6 0.24 0.00 0.58 0.58
  7 0.00 0.87 0.00 0.00
  8 0.00 0.74 0.46 0.46
  </code></pre>
  
  <p>What I would like to get is this:</p>
  
  <pre><code>df
      SL MPhL  MPL   SC   GL
  1 0.82 0.95 0.00 0.00 0.82 # When "SL"&gt;0, GL= SL
  2 0.00 0.85 0.50 0.50 0.21 # When "SL" is 0, GL = MPhL*MPL*SC
  3 0.24 0.65 0.10 0.10 0.24
  4 0.00 0.35 0.89 0.89 0.28
  5 0.98 0.15 0.33 0.33 0.98
  6 0.24 0.00 0.58 0.58 0.24
  7 0.00 0.87 0.00 0.00 0.00
  8 0.00 0.74 0.46 0.46 0.16
  </code></pre>