title: R Multiple condition when looping through matrices and storing results with correct date attached
body: <p>I started with this matrix exercise but quickly realised that I could't find an elegant way to facilitate what I have planned:</p>
  
  <p>I have three matrices (AB, CD and MN)</p>
  
  <pre><code>require(xts)
  set.seed(21)
  A &lt;- xts(matrix(rnorm(20,0,0.01),2,1), Sys.Date()-2:1)
  colnames(A) &lt;- c("A")
  B &lt;- xts(matrix(rnorm(20,0,0.01),2,1), Sys.Date()-2:1)
  colnames(B) &lt;- c("B")
  AB &lt;- cbind(A,B)
  
  C &lt;- xts(matrix(rnorm(20,0,0.01),2,1), Sys.Date()-2:1)
  colnames(C) &lt;- c("C")
  D &lt;- xts(matrix(rnorm(20,0,0.01),2,1), Sys.Date()-2:1)
  colnames(D) &lt;- c("D")
  CD &lt;- cbind(C,D)
  
  M &lt;- xts(matrix(rnorm(20,0,0.01),2,1), Sys.Date()-2:1)
  colnames(M) &lt;- c("M")
  N &lt;- xts(matrix(rnorm(20,0,0.01),2,1), Sys.Date()-2:1)
  colnames(N) &lt;- c("N")
  MN &lt;- cbind(M,N)
  </code></pre>
  
  <p>Now I want to compare the values of matrix AB with the values in matrix CD. If the value in AB is larger than the value in CD, I want to multiply the value in MN by 1 and if the value in AB is smaller than that in CD, I want to multiply the corresponding value in MN by 0. </p>
  
  <p>The results are supposed to be stored in matrix XY. Instead of writing an 'if else function' for A and B (since I have much more objects to look at), I am looking for a nice loop or apply rule. </p>
  
  <p>For illustration here is the desired result</p>
  
  <pre><code>&gt; AB
                        A             B
  2014-11-28 0.0022296815  0.0005334298
  2014-11-29 0.0001194865 -0.0052610564
  
  &gt; CD
                       C            D
  2014-11-28 -0.01804041 -0.002367161
  2014-11-29 -0.00964183  0.003592538
  
  &gt; MN
                       M           N
  2014-11-28 -0.01267330 -0.01180904
  2014-11-29  0.00199446 -0.01228188
  
  &gt; XY
                       X            Y
  2014-11-28 -0.01267330  -0.01180904
  2014-11-29 0.00199446   0.00000000
  </code></pre>
  
  <p>Further I couldn't figure out how to properly keep the dates attached. 
  Thanks a lot.</p>