title: Cbind/Rbind With Ifelse Condition
body: <p>Here is the code that I am working with: </p>
  
  <pre><code>x &lt;- c("Yes","No","No","Yes","Maybe")
  y &lt;- t(1:10)
  z &lt;- t(11:20)
  
  rbind.data.frame(ifelse(x == "Yes",y,z))
  </code></pre>
  
  <p>This produces </p>
  
  <pre><code>    X1L X12L X13L X4L X15L
  1   1   12   13   4   15
  </code></pre>
  
  <p>The desired outcome is: </p>
  
  <pre><code>x
  1   Yes   1    2    3    4    5    6    7    8    9    10
  2    No   11   12   13   14   15   16   17   18   19    20
  3    No   11   12   13   14   15   16   17   18   19    20
  4   Yes   1    2    3    4    5    6    7    8    9    10
  5 Maybe   11   12   13   14   15   16   17   18   19    20
  </code></pre>
  
  <p>I was thinking that I could use an ifelse statement with the rbind.data.frame() or cbind.data.frame() function. So if <code>x == "Yes"</code> then that would be combined with a vector "y". As shown in the first row of the desired output. Inversely if <code>x!="Yes"</code> then it would be combinded with the vector "z". How would I go about doing this. I also thought maybe indexing with the which() function could be possible but I could not think of how I would use it. </p>
  
  <p><strong>UPDATE ANOTHER QUESTION</strong></p>
  
  <p>Here is the code I am working with :</p>
  
  <pre><code>    a &lt;- c(1,0,1,0,0,0,0)
      b &lt;- 1:7 
  
      t(sapply(a, function(test) if(test==0) b else 0))
  
  Which produces 
  
          [,1] [,2]      [,3] [,4]      [,5]      [,6]      [,7]     
      [1,] 0    Integer,7 0    Integer,7 Integer,7 Integer,7 Integer,7
  </code></pre>
  
  <p>Can any of you explain this? The code  below works but I was wondering why the sapply function does not work. Also how would I save the matrix that is created below? </p>
  
  <pre><code>`row.names&lt;-`(t(t(rbind(b,0))[,(a!='1')+1L]),x) 
  </code></pre>
  
  <p><strong>Answer To My Most Recent Question</strong></p>
  
  <p>For the sapply function to work it needs the input in the else statement to be a vector so, </p>
  
  <pre><code>        a &lt;- c(1,0,1,0,0,0,0)
          b &lt;- 1:7 
          c &lt;- rep(0, times = length(a))
          t(sapply(a, function(test) if(test==0) b else c))
  </code></pre>
  
  <p>Now this produces the proper output. </p>