使用while函数制作Z表

问题描述 投票:1回答:1

[我们班上的老师在教我们关于while构造的同时,举例说明了如何使用乘法表,如下所示:

A <- matrix(nrow = 10, ncol = 10)
i=1
j=1
while (i<=10) {while (j<=10) {A[i,j]=i*j
j=j+1}
i=i+1
j=1  
}
A

因此,我尝试使用该函数但参数范围为0-0.99的Z表。但是,我不确定以下代码是否正确:

B <- matrix(nrow = 10, ncol = 10)

i=0
j=0
while (i<=0.9) {while (j<=0.09) {B[i,j]=integrate[(1/sqrt(2*pi))*exp^((-(mean(i+j)^2)/2))]
j=j+0.01
i=0
}
i=i+1
j=0
}
B
"Error in exp^((-(mean(i + j)^2)/2)) : 
  non-numeric argument to binary operator"

如果您能帮助我如何使用while构造该Z表,我将非常感激。谢谢..

r statistics
1个回答
1
投票
Z_table <- function(zmax=0.99, decimal=4){ i <- 1; j <- 1 z1.max <- trunc(zmax*10)/10 # First decimal place of z z2.max <- trunc(zmax*100)/100 - z1.max # Second decimal place of z n <- length(seq(0, z1.max, 0.1)) * 10 B <- matrix(rep(0, n), ncol=10) colnames(B) <- c("0.00", seq(0.01, 0.09, by=0.01)) rownames(B) <- c("0.0", seq(0.1, z1.max, by=0.1)) z1 <- 0 while(z1 <= z1.max) { z2 <- 0 while(z2 <= 0.09) { B[i, j] <- round(integrate(dnorm, -Inf, z1 + z2)$value, decimal) z2 <- z2 + 0.01 j <- j + 1 } i <- i + 1 z1 <- round(z1 + 0.1, 1) # takes care of rounding errors j <- 1 } cat("Probabilities are between -Inf and z\n") B }

> Z_table()
Probabilities are between -Inf and z
      0.00   0.01   0.02   0.03   0.04   0.05   0.06   0.07   0.08   0.09
0.0 0.5000 0.5040 0.5080 0.5120 0.5160 0.5199 0.5239 0.5279 0.5319 0.5359
0.1 0.5398 0.5438 0.5478 0.5517 0.5557 0.5596 0.5636 0.5675 0.5714 0.5753
0.2 0.5793 0.5832 0.5871 0.5910 0.5948 0.5987 0.6026 0.6064 0.6103 0.6141
0.3 0.6179 0.6217 0.6255 0.6293 0.6331 0.6368 0.6406 0.6443 0.6480 0.6517
0.4 0.6554 0.6591 0.6628 0.6664 0.6700 0.6736 0.6772 0.6808 0.6844 0.6879
0.5 0.6915 0.6950 0.6985 0.7019 0.7054 0.7088 0.7123 0.7157 0.7190 0.7224
0.6 0.7257 0.7291 0.7324 0.7357 0.7389 0.7422 0.7454 0.7486 0.7517 0.7549
0.7 0.7580 0.7611 0.7642 0.7673 0.7704 0.7734 0.7764 0.7794 0.7823 0.7852
0.8 0.7881 0.7910 0.7939 0.7967 0.7995 0.8023 0.8051 0.8078 0.8106 0.8133
0.9 0.8159 0.8186 0.8212 0.8238 0.8264 0.8289 0.8315 0.8340 0.8365 0.8389

> Z_table(zmax=2.9, decimal=3)
© www.soinside.com 2019 - 2024. All rights reserved.