【发布时间】:2020-07-22 13:11:41
【问题描述】:
下面的代码在 R Studio 中生成了几乎像动画一样的内容,因为它渲染了 100 个图,每个图的数据都比前一个图略多。
################################################################################
# Visualise probability of heads tending towards 0.5 as more tests performed
# Number of tests to run
tests <- 100
# duration to run for in seconds
durationSeconds <- 10
ht <- sample(c('heads', 'tails'), tests, replace=TRUE)
total <- vector()
for (i in 1:tests) {
headsAtI <- length(which(ht[1:i] == 'heads'))
total[i] <- headsAtI/i
Sys.sleep(durationSeconds/tests)
plot(total, type='l')
abline(h = 0.5, col='blue')
}
这可行,但有一些严重的问题:
- 创建了 100 个地块,我想理想情况下应该创建并重复使用一个地块
- 如果我将“测试”的值更改为 10,000,R Studio 将挂起或崩溃
在 R Studio 中执行此操作的正确方法是什么?
我意识到我可以在最后只画一个情节,包含所有“结果”,但我想实现“动画”效果。
【问题讨论】:
标签: r