The problem is: If a teacher makes a bet with his class of 35 people "how many people have the same birthday?". If he loses the bet he pays $2, if he wins the bet, he gets $1. He did this bet 1137 times. So, I did a loop to represent this. Now, I want to repeat this test 1000 times and save it in a vector or table, so I can then run an inferential test on it. How do I repeat the loop?
```{r}
exact=c(seq(1:365), seq(1:365), seq(1:365), seq(1:366))
bucket=numeric()
smithbucks=0
for(i in 1:1137){
class=sample(exact, size = 35, replace = TRUE)
hmmm=sum(duplicated(class))
if (hmmm > 0) smithbucks = smithbucks +1
else smithbucks = smithbucks -2}
```
Lots of ways to do it. One simple way is to put it inside a sapply loop as such:
results <- sapply(1:1000, function(i) {
exact=c(seq(1:365), seq(1:365), seq(1:365), seq(1:366))
smithbucks=0
for(i in 1:1137){
class=sample(exact, size = 35, replace = TRUE)
hmmm=sum(duplicated(class))
if (hmmm > 0) smithbucks = smithbucks +1
else smithbucks = smithbucks -2}
return(smithbucks)
})
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.