r - R while 循环未按预期运行
问题描述
我需要创建一个包含 30 个单位的样本,计算批次 ( lote
)、批次中的单位 ( n
)、平均值 ( media
)、标准偏差 ( sd
) 和幅度 ( amplitud
),并创建一个包含单位。
我生成第一个样本
tabla <- c(numeric())
columnas <- c("Lote", "n", "Media", "Dt", "Amplitud")
datos <- data.frame(matrix(c(numeric),ncol = 5,nrow =0, byrow = T ))
colnames(datos) <- columnas
lote <- 0
size <- 30
z <- qt(0.975,df = 30-1)
d <- 10
so <- sqrt(var(tabla))
error <- (so/sqrt(size))
amplitud <- (2* z * error)
fila <- 1
set.seed(37)
matriz <- matrix(c(numeric()),byrow = T, ncol = 1)
# I create the first sample:
for(i in 1:size){
c1 <- rnorm(1,9.5 ,1)
c2 <- rpois(1,(5*c1))
c3 <- rexp(1,7)
c4 <- rexp(1,16)
c <- c1*c2 + c3*c4
matriz <- c(matriz,c)
}
tabla <- c(matriz)
so <- sqrt(var(tabla))
error <- (so/sqrt(size))
amplitud <- (2* z * error)
datos[fila,1] <- lote
datos[fila,2] <- size
datos[fila,3] <- mean(matriz)
datos[fila,4] <- sqrt(var(matriz))
datos[fila,5] <- (2* z * error)
lote <- lote + 1
fila <- fila + 1
size <- round(((2*z)*(so/d))^2)
然后我需要不断添加批次 ,并在每次迭代后size <- round(((2*z)*(so/d))^2)
计算幅度。(2* z * error)
如果幅度是< d
, (d <- 10)
,我需要打破循环。
我写了这个:
while(condicion){
for(i in 1:size){
matriz <- matrix(c(numeric()),byrow = T, ncol = 1)
for(i in 1:size){
c1 <- rnorm(1,9.5 ,1)
c2 <- rpois(1,(5*c1))
c3 <- rexp(1,7)
c4 <- rexp(1,16)
c <- c1*c2 + c3*c4
matriz <- c(matriz,c)
}
tabla <- c(matriz)
so <- sqrt(var(tabla))
error <- (so/sqrt(size))
size <- round(((2*z)*(so/d))^2)
amplitud <- round((2* z * error))
datos[fila,1] <- lote
datos[fila,2] <- size
datos[fila,3] <- mean(matriz)
datos[fila,4] <- sqrt(var(matriz))
datos[fila,5] <- (2* z * error)
lote <- lote + 1
fila <- fila + 1
}
if(amplitud < d){
condicion <- FALSE
break
}
}
该程序给了我一个输出,但它应该在第 5 次迭代后停止,但它一直在继续......
Lote n Media Dt Amplitud
1 0 30 432.9111 90.7863 67.80033
2 1 2177 455.905 114.0621 12.5641
3 2 2197 459.2841 114.5811 10.04514
4 3 2358 455.4 118.7037 10.35908
5 4 2256 458.2974 116.1051 9.780284
6 5 2226 454.3239 115.3553 9.934368
7 6 2313 457.0264 117.5647 10.19263
[ reached getOption("max.print") -- omitted 3398 rows ]
为什么我的循环第一次没有中断amplitud < d
?
解决方案
推荐阅读
- azure - Azure Function App 链接授权
- python - 在 pytest 中获取测试执行的唯一 ID
- c# - 减小视频 Xamarin 表单的大小
- c# - RSA(RFC 4716)RSAParameter dotNet c#中的公钥
- etl - 如何在 Informatica Data Hub 中过滤属性
- javascript - NuxtJS 在使用 Nuxt-Link 访问路由时预取数据?
- reactjs - 反应 clearinterval 不适用于另一个功能
- javascript - internal/modules/cjs/loader.js:960 错误:找不到模块'C:\Users\Sanjai\AppData\Roaming\npm\node_modules\protractor\bin\protractor'
- android - Android 在所有活动中更改语言
- java - Apache 点燃备份副本分发