title: xts - Delete rows based on certain criterias
body: <p>I need to delete xts rows based on certain criterias in column [code]. It is fine that by deleting there will be time-gaps in the xts time series.</p>
  
  <p><strong>Question:</strong> How do I solve step1/step3/step4.</p>
  
  <hr>
  
  <p>The criterias are as following:</p>
  
  <p><strong>Step-1: Value [3] in [code]:</strong> If xts starts with [code] [3] delete that row. </p>
  
  <p><strong>Step-2: Value [0] in [code]:</strong> Delete the complete row.</p>
  
  <p><strong>Step-3: Value [2] in [code]:</strong> a) Keep only [2] that starts the xts, all lines above the first [2] should be removed.
  b) Keep [2] that has a [3] above itself. </p>
  
  <p><strong>Step-4: Value [3] in [code]:</strong> Keep only [3] that has a [2] above itself. </p>
  
  <p>My solution for step-2: </p>
  
  <p>Finds and keeps, all [2] and [3], thus removing all [0]:</p>
  
  <pre><code>xts3 &lt;- xts3[grep("[2]|[3]", xts3$code), ] 
  </code></pre>
  
  <p>My R-file:</p>
  
  <pre><code>dates &lt;- as.POSIXct( # Construct the dates to be used.
    c(
    "2013-07-24 09:01:00", 
    "2013-07-24 09:02:00", 
    "2013-07-24 09:03:00",
    "2013-07-24 09:04:00",
    "2013-07-24 09:05:00",
    "2013-07-24 09:06:00",
    "2013-07-24 09:07:00",
    "2013-07-24 09:08:00",
    "2013-07-24 09:09:00"
    )
    )
  
  code &lt;- c(3, 2, 0, 2, 2, 2, 3, 3, 3) # Criterias for delete/keep rows.
  
  data &lt;- data.frame(code) # Create a dataframe.
  
  xts3 &lt;- xts(x=data, order.by=dates) # Create xts based on dataframe.
  </code></pre>
  
  <p>The result of the R-file (prior to deleting rows based on criterias):</p>
  
  <pre><code>                    code
  2013-07-24 09:01:00    3
  2013-07-24 09:02:00    2
  2013-07-24 09:03:00    0
  2013-07-24 09:04:00    2
  2013-07-24 09:05:00    2
  2013-07-24 09:06:00    2
  2013-07-24 09:07:00    3
  2013-07-24 09:08:00    3
  2013-07-24 09:09:00    3
  </code></pre>
  
  <p>Explanation: What should trigger delete of rows (based on criterias):</p>
  
  <pre><code>                    code
  2013-07-24 09:01:00    3 # To be removed due to step-1.
  2013-07-24 09:02:00    2 # To be kept due to step-3a.
  2013-07-24 09:03:00    0 # To be removed due to step-2
  2013-07-24 09:04:00    2 # To be removed due to not fulfilling step-3b
  2013-07-24 09:05:00    2 # To be removed due to not fulfilling step-3b
  2013-07-24 09:06:00    2 # To be removed due to not fulfilling step-3b
  2013-07-24 09:07:00    3 # The kept due to step-4
  2013-07-24 09:08:00    3 # To be removed due to not fulfilling step4.
  2013-07-24 09:09:00    3 # To be removed due to not fulfilling step4.
  </code></pre>
  
  <p>Expected outcome after deleting rows has been done:</p>
  
  <pre><code>                    code
  2013-07-24 09:02:00    2
  2013-07-24 09:07:00    3
  </code></pre>