[R] Nested Loop in R

Ko-Kang Kevin Wang k.wang at auckland.ac.nz
Tue Apr 6 10:48:48 CEST 2004


Thanks for those who replied!

I found out the problem.  I don't need the nested loop at all.  The
following code would've worked (for those who're interested to know):
#####
## Testing CLT.  pp 281 of Chance Encounters
x <- round(runif(1000, 1, 6))
clt <- function(x, samp.no = 5, n = 5) {
  samp <- vector(mode = "list", length = samp.no)
  plot.new()
  plot.window(xlim = c(1, 6), ylim = c(1, samp.no))
  axis(1, at = 1:6)
  axis(2, at = 1:samp.no, labels = samp.no:1, las = 1)

  for(i in 1:samp.no) {
    samp[[i]] <- sample(x, n)
    points(jitter(samp[[i]], 0.3), rep(i, n))
    lines(x = c(mean(samp[[i]]), mean(samp[[i]])),
          y = c(i - 0.1, i + 0.1))
  }
  title(main = paste(samp.no, " samples of size n = ", n, sep = ""),
        xlab = "6-Sided Die",
        ylab = "Sample Number")
}

## 5 Samples
par(mfrow = c(2, 2), bg = "white")
clt(x, samp.no = 5, n = 5)
clt(x, samp.no = 5, n = 10)
clt(x, samp.no = 5, n = 20)
clt(x, samp.no = 5, n = 30)
#####




More information about the R-help mailing list