title: how to create a data frame with rows and columns for a function in R?
body: <p>I was assigned a question that requires me to create a data frame for a function and I do not know how to proceed with it.</p>
  
  <blockquote>
    <p>To evaluate the stay and switch strategies we must repeat the game many 
    times to determine the proportion of wins based on each strategy. To do this, 
    write another function called <code>monty_hall()</code> which has one argument, <code>iter</code>, the 
    number of iterations, and which returns a data frame with <code>iter</code> rows and five 
    columns. This function should call the <code>monty_hall_one_play()</code> function.
    You will probably want to write some type of loop or try <code>replicate()</code>.</p>
    
    <p>The data frame columns that function <code>monty_hall()</code> should return are:</p>
    
    <ul>
    <li><p><code>car_door</code>, gives the door behind which the car is hidden.</p></li>
    <li><p><code>contestant_door</code>, gives the door initially selected by the contestant.</p></li>
    <li><p><code>host_door</code>, gives the door revealed by the host.</p></li>
    <li><p><code>switch</code>, result of switch strategy; 1 is win, 0 is loss.</p></li>
    <li><p><code>stay</code>, result of stay strategy; 1 is win, 0 is loss.</p></li>
    </ul>
  </blockquote>
  
  <p>So far, my code looks like</p>
  
  <pre><code>monty_hall &lt;- function(iter) {
    replicate(iter,monty_hall_one_play())
  }
  monty_hall(5)
  </code></pre>
  
  <p>My code for the function monty_hall_one_play is</p>
  
  <pre><code>monty_hall_one_play &lt;- function() {
    first &lt;- sample(x = c(1:3),size = 1,replace = FALSE,prob = c(1/3,1/3,1/3))
    second &lt;- sample(x = c(1:3),size = 1,replace = FALSE,prob = c(1/3,1/3,1/3))
  
    if (first == 1 &amp; second == 1) {
      third = sample(c(2,3),1)
    }
    if (first == 2 &amp; second == 2) {
      third = sample(c(1,3),1)
    }
    if (first == 3 &amp; second == 3) {
      third = sample(c(1,2),1)
    }
    if (first == 1 &amp; second == 2) {
      third = 3
    }
    if (first == 2 &amp; second == 3) {
      third = 1
    }
    if (first == 3 &amp; second == 1) {
      third = 2
    }
    if (first == 2 &amp; second == 1) {
      third = 3
    }
    if (first == 3 &amp; second == 2) {
      third = 1
    }
    if (first == 1 &amp; second == 3) {
      third = 2
    }
  
    return(c(first,second,third))
  }
  </code></pre>
  
  <p>"First,""second," and "third" correlate to assigned elements.</p>
  
  <p>These were the directions:</p>
  
  <blockquote>
    <p>Write a function called <code>monty_hall_one_play()</code> that takes no arguments
    and returns a numeric vector with each element representing the following:</p>
    
    <ul>
    <li><p>the first element is the door behind which the car is hidden 
    (selected randomly from <code>1:3</code>)</p></li>
    <li><p>the second element is the door initially selected by the contestant 
    (selected randomly from <code>1:3</code>)</p></li>
    <li><p>the third elment is the door revealed by the host</p></li>
    <li><p>if the contestant selected the door with the car, the host randomly 
    selects one of the two remaining doors</p></li>
    <li><p>if the contestant selected a door different from the door with the car, 
    the host selects the door that is not hiding the car</p></li>
    </ul>
  </blockquote>
  
  <p>I am unsure as to how I create the data frame and the function itself.</p>