我试图生成随机数据。基本上,我从书中复制了这段代码,但它并不适合我。它一直有效,直到达到警告中所述的行:
Error: unexpected '}' in:
"+ this.seg[,j] <- rnorm(segSize[i], mean=segMeans[i,j], sd=segSDs[i,j])
+ }"
代码如下:
for (i in seq_along(segNames)) {
+ cat(i, segNames[i], "\n")
+
+ # empty matrix to hold this particular segment’s data
+ this.seg <- data.frame(matrix(NA, nrow=segSize[i], ncol=length(segVars)))
+
+ # within segment, iterate over variables and draw appropriate random data
+ for (j in seq_along(segVars)) { # and iterate over each variable
+ if (segVarType[j] == "norm") { # draw random normals
+ this.seg[,j] <- rnorm(segSize[i], mean=segMeans[i,j], sd=segSDs[i,j])
+ } else if (segVarType[j] == "pois") { # draw counts
+ this.seg[, j] <- rpois(segSize[i], lambda=segMeans[i, j])
+ } else if (segVarType[j] == "binom") { # draw binomials
+ this.seg[, j] <- rbinom(segSize[i], size=1, prob=segMeans[i, j])
+ } else {
+ stop("Bad segment data type: ", segVarType[j])
+ }
+ }
+ # add this segment to the total dataset
+ seg.df <- rbind(seg.df, this.seg)
+ }
有人可以解释为什么它会停留在那条线上并且需要改变什么?谢谢!
答案 0 :(得分:1)
您必须在没有&#34; +&#34;的情况下复制代码。标志......然后代码有效!