Skip to content
Prev 295192 / 398506 Next

Loop Help

Here is a slightly different approach that takes advantage of recycling:

# Make 7 data frames
for (i in 1:7) {
  assign(paste("TOWER", i, sep=""), data.frame(A=letters[1:4],
    X=rnorm(4)))
}
# Add Tower column taking advantage of recyling
tnames <- paste("TOWER", 1:7, sep="")
for (i in 1:7) {
  assign(tnames[i], cbind(eval(as.name(tnames[i])), Tower=i))
}
# Combine them into a single data frame
TOWER <- do.call(rbind, lapply(tnames, as.name))