I'm really new to R and I have encountered the error above when running my code for Q6:
- Question 5: Write a function that simulates $X_n$ for a given party size $n$. That is, your function should take the party size $n$ as input, shuffle the hats (i.e., a vector of IDs), then count how many people got their hat back and return this count as an output. \newline Hint: The sampleorsample.intfunctions may be useful for this exercise.
#Replace with your code for Question 5
hat_problem <- function(n) {
  x <- 1:n
  set.seed(123)
  x_n <- sample(x)
  count(which(x == x_n))
}
- Question 6: Simulate $X_n$ 1,000 times for $n=30$ and plot a frequency barplot of the (simulated) distribution of $X_n$ (either using ggplot2, or the base Rplot, up to you). \newline Hint: Thereplicatefunction will be useful for this exercise, though ansapplyor aforloop would also work.
#Replace with your code for Question 6
hat <- replicate(n = 1000, hat_problem(30))
ggplot(hat, aes(x, y)) + geom_bar()
Did I misuse the replicate() or count()? How should I fix it?
