【发布时间】:2015-06-19 04:40:52
【问题描述】:
我对 R 很陌生。当值设置得较低时,我的脚本可以正常工作。我需要计算概率并且需要至少 1,000,000 个样本。我的第一个循环返回此错误:
Error in new.matrix[i, ] <- as.vector(table(million.rep[, i])) : number of items to replace is not a multiple of replacement length
我的脚本:
actual.prob <- c(.14, .14, .16, .13, .19, .24)
number.in.bag <- c(8,6,20,9,10,5)
million.rep <- replicate(1000000, sample(1:6, 58, replace= T, actual.prob))
new.matrix <- matrix(nrow= 1000000, ncol=6)
# Note to other readers... the above line throws the error.
for(i in 1:1000000){
new.matrix[i,] <- as.vector(table(million.rep [, i]))
}
y <- c()
for(i in 1:1000000){
y[i] <- sum(new.matrix[i,] == number.in.bag)
}
y
sum(y == 6)
【问题讨论】: