我对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
Run Code Online (Sandbox Code Playgroud)
我的剧本:
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 …Run Code Online (Sandbox Code Playgroud) I have about 10000 replicates of a sample in a matrix. My matrix has 1000 rows and 6 columns. Numbers in the columns range from 0:58 depending on the sample.
actual.prob <- c(.14, .14, .16, .13, .19, .24)
million.rep <- replicate(10000, sample(1:6, 58, replace= T, actual.prob))
new.matrix <- matrix(nrow= 10000, ncol=6)
for(i in 1:10000){
new.matrix[i,] <- as.vector(table(factor(million.rep[,i], levels=1:6)))
}
new.matrix[1:10,]
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 3 7 11 11 11 15
[2,] 7 6 12 5 19 9 …Run Code Online (Sandbox Code Playgroud)