title: Converting for loop to function and applying to list of dataframes
body: <p>I previously created a for loop to identify periods when water level decreased. This worked fine for smaller continuous time series data.</p>
  <pre><code>library(tidyverse)
  library(lubridate)
  
  level_data &lt;- c(10:4, 20:9, 16:5, rep(0, 3))           
  times_stamp &lt;- seq(ymd_hms('2015-07-22 12:15:00'), ymd_hms('2015-07-22 20:30:00'), by = '15 mins')
  precip_data &lt;- c(rep(0, 10), 1:4, rep(0, 10), 1:5, rep(0, 5))
  
  maxP.neg &lt;- .1
  
  # Create objects for holding the start and end dates. These lists should end up 
  # the same length, so that each start date has a corresponding end date.
  
  startDates &lt;- c()                                     
  endDates &lt;- c()
  
  recede &lt;- 0                                           # This is a switch to keep track of whether a recession period is in progress
  
  for (i in 2:length(level_data)) {                     # i.e. start at the second data point
   diffQ &lt;- level_data[i] - level_data[i - 1]          # Calculate difference between current and previous timestamp
   
   if (diffQ &lt; 0 &amp;&amp;                                    # If difference is negative (i.e. receding) AND
       recede == 0 &amp;&amp;                                  # a recession period has not already begun (recede == 0) AND
       precip_data &lt;= maxP.neg) {                      # min. dry period criteria is met ...
     startDates &lt;- append(startDates, times_stamp[i])  # Record the start time of the recession period
     recede &lt;- 1                                       # Change recede to 1 to indicate a recession period has begun
     
   } else if (diffQ &gt;= 0 &amp;&amp;                            # If the difference becomes positive 
              recede == 1) {                           # and a recession period was in progress...
     endDates &lt;- append(endDates, times_stamp[i - 1])  # Record the previous timestamp as the end date of the recession     
     recede &lt;- 0                                       # Set recede back to 0 to show the recession period has ended                             
     
   } else {                                            # Otherwise just continue to the next data point.
     next                                     
   }
  }
  </code></pre>
  <p>However, I want to perform the same analysis over a larger dataset where there are time breaks. I thought to split the data into a list of dataframes and use <code>lapply</code> and a custom function.</p>
  <p>This is what I have come up with but I am not getting the same output as the for loop method?</p>
  <pre><code>diff_Q &lt;- diff(level_data)
  
  date_time1 &lt;-
    seq(ymd_hms('2015-07-22 12:15:00'),
        ymd_hms('2015-07-22 20:15:00'),
        by = '15 mins')
  date_time2 &lt;-
    seq(ymd_hms('2015-07-25 08:00:00'),
        ymd_hms('2015-07-25 16:00:00'),
        by = '15 mins')
  
  cum_precip &lt;- c(rep(0, 10), 1:4, rep(0, 10), 1:5, rep(0, 4))
  
  df1 &lt;-
    data.frame(date_time1, diff_Q, cum_precip) %&gt;% rename(date_time = date_time1)
  df2 &lt;-
    data.frame(date_time2, diff_Q, cum_precip) %&gt;%  rename(date_time = date_time2)
  
  recede_ls &lt;- list(df1, df2)
  
  startDates2 &lt;- c()
  endDates2 &lt;- c()
  
  RA.function &lt;- function(x) {
    recede &lt;- 0
    
    if (diff_Q &lt; 0 &amp;&amp;
        recede == 0 &amp;&amp;
        cum_precip &lt;= maxP.neg) {
      startDates2 &lt;- x$date_time
      recede &lt;- 1
    } else if (diffQ &gt;= 0 &amp;&amp;
               recede == 1) {
      endDates2 &lt;- x$date_time[-1]
      recede &lt;-
        0
      
    } else {
      next
    }
  }
  
  lapply(recede_ls, RA.function)
  </code></pre>
  <p>Thanks for your help!</p>