title: How do you add rows that have NA as prior columns in R?
body: <p>All,</p>
  
  <p>I have a dataframe with Dates on the first column and categories across as such:</p>
  
  <pre><code>Accounts &lt;- c('A','B','C','D',
            'A','B','C','D',
            'A','B','C','D')
  Dates &lt;- as.Date(c('2016-01-31', '2016-01-31','2016-01-31','2016-01-31',
                 '2016-02-28','2016-02-28','2016-02-28','2016-02-28',
                 '2016-03-31','2016-03-31','2016-03-31','2016-03-31'))
  Balances &lt;- c(100,NA,NA,NA,
            90,50,10,NA,
            80,40,5,120)
  Origination &lt;- data.frame(Dates,Accounts,Balances)
  
  library(reshape2)
  Origination &lt;- dcast(Origination,Dates ~ Accounts, value.var = "Balances")
  
         Dates   A  B  C   D
  1 2016-01-31 100 NA NA  NA
  2 2016-02-28  90 50 10  NA
  3 2016-03-31  80 40  5 120
  </code></pre>
  
  <p>The goal is to sum rows where the prior values is NA.  I tried to used lag or shift but don't have the knowledge to pull it off.</p>
  
  <p>So for this dataframe I would like a Totals column at the end it values 60 (50 + 10) and 120 for February and March.</p>
  
  <p>Is this doable?</p>
  
  <p>Regards,
  Aksel</p>