title: How to duplicate rows conditionally and perform arithmetic calculation in a custom function?
body: <p>Im trying to solve a problem where I would like to duplicate some rows conditionally and perform an arithmetic operation on them. Appreciate any suggestions!</p>
  <pre><code>library(dplyr)
  #-----------------------------------------------
  # Dummy Data
  #----------------------------------------------
  emp_id &lt;- c(1,2,3,4, 5, 6)
  employee &lt;- c('John Doe','Peter Gynn','Jolie Hope','Michael K', 'T  Big', 'Joo Pite')
  salary &lt;- c(21000, 23400, 26800, 1000, 2000, 1500)
  date &lt;- c('2010-11-01','2010-11-02','2010-11-03', '2010-11-04', '2010-11-05', '2010-11-06')
  status &lt;- c('no','yes','no','no','no','yes')
  
  
  employ_data &lt;- data.frame(emp_id, employee, salary, date, status)
  </code></pre>
  <p>Output:</p>
  <pre><code>    emp_id    employee       salary     date         status
      1          John Doe      21000      2010-11-01  no
      2          Peter Gynn    23400      2010-11-02  yes
      3          Jolie Hope    26800      2010-11-03  no
      4          Michael K     1000       2010-11-04  no
      5          T Big         2000       2010-11-05  no
      6          Joo Pite      1500       2010-11-06  yes
  
  
  in_date_range = seq(as.Date(min(employ_data$date)),by = 1, 
  as.Date(max(employ_data$date)))
  
  duplicate_and_adjust_row &lt;- function(x){
    result &lt;–rep(x,length(.))
    result$salary * -1
  }
  
  employ_data %&gt;%
    rowwise() %&gt;%
    mutate(salary = ifelse(status == 'yes' &amp; date %in% in_date_range, 
   duplicate_and_adjust_row(x), salary))
  </code></pre>
  <p>Expected Output:</p>
  <pre><code>    emp_id    employee       salary     date         status
      1          John Doe      21000      2010-11-01  no
      2          Peter Gynn    23400      2010-11-02  yes
      3          Jolie Hope    26800      2010-11-03  no
      4          Michael K     1000       2010-11-04  no
      5          T Big         2000       2010-11-05  no
      6          Joo Pite      1500       2010-11-06  yes
      2          Peter Gynn   -23400      2010-11-02  yes
      6          Joo Pite     -1500       2010-11-06  yes
  </code></pre>