[R] create an object list in a loop

Ott Toomet siim at obs.ee
Mon Oct 28 15:06:37 CET 2002


On Mon, 28 Oct 2002 AlessandroSemeria at cramont.it wrote:

  |Hi! Probably I perform this question because I did'nt  still understand the
  | I have to build  many matrix, with different dimensions, and  I would to
  |assign them a
  |same 'prefix' name, i.e. "aval", but, obviuosly different suffix, something
  |for (i in 1:n) {
  |     aval%i% <- matrix(scan(data....),nrow=nr[i],ncol=nc[i]
  |          }
  |where "%i%" is a  symbol to indicate different label for different index i.
  |Ther'is some trick to "paste" a label(i)  and to build the object aval%i%
  |or some other way
  |to perform my task?

There is two solutions:

a) use assign:

assign(paste("aval", i, sep="")) <- matrix(...)

b) use lists.  I would prefer this one as it is more in line with R
philosophy, and the components are more easy to handle later:

aval <- vector("list", n)
for(i in 1:n)
   aval[[i]] <- matrix(...)

you can later access the components as aval[[1]]



Ott Toomet
otoomet at econ.au.dk


 (o_         (*_         (O_         (o< -!      (o<)<
//\         //\         //\         //\         //\
V_/_        V_/_        V_/_        V_/_        V_/_

standard    drunken     shocked     noisy      penguin
penguin     penguin     penguin     penguin    eating fish

r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch

More information about the R-help mailing list