生日悖论功能
我是R的初学者,正在试图创建生日悖论功能并设法达到这一点,结果约为0.5。
k <- 23
sims <- 1000
event <- 0
for (i in 1:sims) {
days <- sample(1:365, k, replace = TRUE)
days.unique <- unique(days)
if (length(days.unique) < k) {
event <- event + 1 }
answer <- event/sims}
answer
但是,当我试图将其放入功能中时,结果始终为0.001。这是代码:
bdayfunction<- function(k){
sims <- 1000
event <- 0
for (i in 1:sims) {
days <- sample(1:365, k, replace = TRUE)
days.unique <- unique(days)
if (length(days.unique) < k) {
event <- event + 1 }
answer <- event/sims
return (answer)
}
}
我做错了什么?
I'm a beginner in R and am trying to create a birthday paradox function and managed to reach this point, and the result is approximately 0.5, as expected.
k <- 23
sims <- 1000
event <- 0
for (i in 1:sims) {
days <- sample(1:365, k, replace = TRUE)
days.unique <- unique(days)
if (length(days.unique) < k) {
event <- event + 1 }
answer <- event/sims}
answer
However, when I tried to put that into a function, the result was always 0.001. Here is the code:
bdayfunction<- function(k){
sims <- 1000
event <- 0
for (i in 1:sims) {
days <- sample(1:365, k, replace = TRUE)
days.unique <- unique(days)
if (length(days.unique) < k) {
event <- event + 1 }
answer <- event/sims
return (answer)
}
}
What have I done wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的
返回
不在正确的位置:它在循环中(您的答案
通过方式计算相同)。这起作用:
在R中,您可以使用允许您进行分组操作的库。这两个主要是
data.table
和dplyr
。在这里,您可以尝试创建一个长data.frame,而不是进行循环,然后计算每个模拟的唯一天数,然后计算出低于k
的出现数量。使用dplyr
:在
data.table
中:您可以测试它们提供相同的结果:
现在让我们比较速度:
您可以看到
data.table
比您最初的循环稍快,并且写得更短。Your
return
is not in the right place: it is in the loop (the same holds for youranswer
calculation by the way).This works:
In R, you can make use of libraries that allows you to do grouping operation. The two main ones are
data.table
anddplyr
. Here, instead of doing a loop, you could try to create a long data.frame with all your simulations, to then calculate the unique number of days per simulation and then count the number of occurrence belowk
. Withdplyr
:In
data.table
:You can test that they provide the same result:
Now lets compare the speed:
You see that
data.table
is slightly faster than your initial loop, and shorter to write.