0

So I am very new to this but I'm trying to make bunch of objects with sequential names. Box1 thru Box100 and have each be an object containing c(0,0). My first though is to make a for loop of

for (i in 1:100 ) {
    Box"i" <- c(0,0)
}

obviously Box"i" isn't something that works and I'm having a hard time figuring out how to do this properly. Any help would be appreciated.

Dick McManus
  • 739
  • 3
  • 12
  • 26

2 Answers2

5

@EDi is right. It's much better practice to keep all these objects in one place, like a list. This way, all the variables are confined to one object, and are also in their own environment.

Maybe you'll want something like this instead.

setNames(replicate(100, c(0, 0), simplify = FALSE), paste0("Box", 1:100))
Rich Scriven
  • 97,041
  • 11
  • 181
  • 245
2

You'll need assign for this:

for (i in 1:100 ) {
  assign(paste0('Box', i), c(0,0))
}

However, I cannot recommend this - why clutter your workspace? - You'll probably want to your results in a list.

EDi
  • 13,160
  • 2
  • 48
  • 57
  • I agree you probably want the results in a list or a name vector. See [why is assign bad](http://stackoverflow.com/questions/17559390/why-is-assign-bad) – MrFlick Aug 24 '14 at 20:48