[R] naming list elements

Pierre Kleiber pkleiber at honlab.nmfs.hawaii.edu
Mon Jan 31 21:20:46 CET 2005


Will the following work for you?
    Cheers, Pierre

 > junk <- list()
 > for(i in 1:5) junk[[paste("Item",i,sep="")]] <- runif(4)
 > str(junk)
List of 5
  $ Item1: num [1:4] 0.687 0.508 0.734 0.704
  $ Item2: num [1:4] 0.5497 0.1777 0.4081 0.0129
  $ Item3: num [1:4] 0.768 0.808 0.592 0.697
  $ Item4: num [1:4] 0.650 0.966 0.265 0.461
  $ Item5: num [1:4] 0.9067 0.3396 0.0412 0.3956


Michael Wolosin wrote:
> All -
> 
> Each time through a loop I create a new dataset, which I would like to 
> append to a list object.  Each item of the list should be the data 
> matrix created in that step of the loop; I would like the NAME (or tag) 
> of that list item to be assigned the value of a character string:
> 
> I've tried something like this:
> 
> running.list <- numeric(0)
> for(i in 1:num.files){
> .....
> running.list <- append(running.list, list(this.item.name=newdatamatrix))
> }
> 
> but the name of the item is always "this.item.name" instead of the 
> CONTENTS of the character string called "this.item.name".
> 
> Obviously, this is correct from R's standpoint, but it's not what I want.
> 
> I can force it to do what I want by doing this:
> 
> running.list <- numeric(0)
> for(i in 1:num.files){
> .....
> junk <- list(newdatamatrix)
> names(junk) <- list(this.item.name)
> running.list <- append(running.list, junk)
> }
> 
> but this requires an extra assigment step, and so has two whole copies 
> of the data matrix laying around.  (Some might point out that I'm being 
> ridiculous worrying about memory if I create a list in this way, but 
> that's another question...)
> 
> In some macro languages I've used, there are ways to force the 
> evaluation of a section of code before it is passed on to the function 
> call (in this case, I'd like to evaluate this.item.name and then 
> effectively call the string "list(blah=newdatamatrix)")
> I've tried doing exactly that, i.e.,
> eval(paste("list(",this.item.name,"=newdatamatrix)"),sep="")
> 
> but that doesn't seem to work either.  It seems like I should be able to 
> do this with eval, call, or something...
> 
> Thanks in advance for any help the list has to offer,
> 
> Mike
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! 
> http://www.R-project.org/posting-guide.html
> 

-- 
-----------------------------------------------------------------
Pierre Kleiber, Ph.D       Email: pkleiber at honlab.nmfs.hawaii.edu
Fishery Biologist           Tel: 808 983-5399 / 808 737-7544 (hm)
NOAA FISHERIES - Honolulu Laboratory         Fax: 808 983-2902
2570 Dole St., Honolulu, HI 96822-2396
-----------------------------------------------------------------
  "God could have told Moses about galaxies and mitochondria and
   all.  But behold... It was good enough for government work."




More information about the R-help mailing list