Gian Batayola
Gian Batayola

Reputation: 73

how to create a data frame with rows and columns for a function in R?

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.

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 monty_hall() which has one argument, iter, the number of iterations, and which returns a data frame with iter rows and five columns. This function should call the monty_hall_one_play() function. You will probably want to write some type of loop or try replicate().

The data frame columns that function monty_hall() should return are:

  • car_door, gives the door behind which the car is hidden.

  • contestant_door, gives the door initially selected by the contestant.

  • host_door, gives the door revealed by the host.

  • switch, result of switch strategy; 1 is win, 0 is loss.

  • stay, result of stay strategy; 1 is win, 0 is loss.

So far, my code looks like

monty_hall <- function(iter) {
  replicate(iter,monty_hall_one_play())
}
monty_hall(5)

My code for the function monty_hall_one_play is

monty_hall_one_play <- function() {
  first <- sample(x = c(1:3),size = 1,replace = FALSE,prob = c(1/3,1/3,1/3))
  second <- sample(x = c(1:3),size = 1,replace = FALSE,prob = c(1/3,1/3,1/3))

  if (first == 1 & second == 1) {
    third = sample(c(2,3),1)
  }
  if (first == 2 & second == 2) {
    third = sample(c(1,3),1)
  }
  if (first == 3 & second == 3) {
    third = sample(c(1,2),1)
  }
  if (first == 1 & second == 2) {
    third = 3
  }
  if (first == 2 & second == 3) {
    third = 1
  }
  if (first == 3 & second == 1) {
    third = 2
  }
  if (first == 2 & second == 1) {
    third = 3
  }
  if (first == 3 & second == 2) {
    third = 1
  }
  if (first == 1 & second == 3) {
    third = 2
  }

  return(c(first,second,third))
}

"First,""second," and "third" correlate to assigned elements.

These were the directions:

Write a function called monty_hall_one_play() that takes no arguments and returns a numeric vector with each element representing the following:

  • the first element is the door behind which the car is hidden (selected randomly from 1:3)

  • the second element is the door initially selected by the contestant (selected randomly from 1:3)

  • the third elment is the door revealed by the host

  • if the contestant selected the door with the car, the host randomly selects one of the two remaining doors

  • if the contestant selected a door different from the door with the car, the host selects the door that is not hiding the car

I am unsure as to how I create the data frame and the function itself.

Upvotes: 1

Views: 109

Answers (1)

rpolicastro
rpolicastro

Reputation: 1305

as.data.frame and colnames in your function should be sufficient.

monty_hall <- function(iter) {
   results <- replicate(iter, monty_hall_one_play())
   results <- as.data.frame(results)
   colnames(results) <- c(
      "car_door", "contestant_door", "host_door",
      "switch", "stay"
   )
   return(results)
} 

result

> monty_hall(5)        
  car_door contestant_door host_door switch stay
1        1               3         2      3    1
2        2               2         2      3    2
3        3               1         1      2    3

Or if you want to be tidy about it.

monty_hall <- function(iter) {
   replicate(iter, monty_hall_one_play()) %>%
      as.data.frame %>%
      set_names("car_door", "contestant_door", "host_door", "switch", "stay")
}

Upvotes: 1

Related Questions