Let me preface my question by stressing that I am much less interested in the answer than in learning a way I could have *found the answer myself*. (As helpful as the participants in this list are, I have far too many R-related questions to resolve by posting here, and as I've written before, in my experience the R documentation has not been very helpful, but I remain hopeful that I may have managed to miss some crucial document.) The task I want to accomplish is very simple: to define and sequentially initialize M x N variables *programmatically*, according to two different categories, containing N and M values, respectively. In languages with associative arrays, the typical way to do this is to define a 2-d associative array; e.g. in Perl one could do for $i ( 'foo', 'bar', 'baz' ) { for $j ( 'eenie', 'meenie', 'minie', 'moe' ) { $table{ $i }{ $j } = read_table( "path/to/data/${i}_${j}.dat" ); } } How does one do this in R? In particular, what's the equivalent of the above in R? Most importantly, how could I have found out this answer from the R docs? Many thanks in advance, kj
Is this what you want? x <- list() for (i in c('test', 'some', 'more')){ for(j in c('lv1', 'lv2', 'lv3')){ x[[i]][[j]] <- runif(10) } } x x[['some']][['lv2']] On 11/2/05, kynn@panix.com <kynn@panix.com> wrote:> > > > > > Let me preface my question by stressing that I am much less interested > in the answer than in learning a way I could have *found the answer > myself*. (As helpful as the participants in this list are, I have far > too many R-related questions to resolve by posting here, and as I've > written before, in my experience the R documentation has not been very > helpful, but I remain hopeful that I may have managed to miss some > crucial document.) > > The task I want to accomplish is very simple: to define and > sequentially initialize M x N variables *programmatically*, according > to two different categories, containing N and M values, respectively. > > In languages with associative arrays, the typical way to do this is to > define a 2-d associative array; e.g. in Perl one could do > > for $i ( 'foo', 'bar', 'baz' ) { > > for $j ( 'eenie', 'meenie', 'minie', 'moe' ) { > > $table{ $i }{ $j } = read_table( "path/to/data/${i}_${j}.dat" ); > } > > } > > How does one do this in R? In particular, what's the equivalent of > the above in R? > > Most importantly, how could I have found out this answer from the > R docs? > > Many thanks in advance, > > kj > > ______________________________________________ > R-help@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 >-- Jim Holtman Cincinnati, OH +1 513 247 0281 What the problem you are trying to solve? [[alternative HTML version deleted]]
On 11/2/05 8:24 AM, "kynn at panix.com" <kynn at panix.com> wrote:> > > > > Let me preface my question by stressing that I am much less interested > in the answer than in learning a way I could have *found the answer > myself*. (As helpful as the participants in this list are, I have far > too many R-related questions to resolve by posting here, and as I've > written before, in my experience the R documentation has not been very > helpful, but I remain hopeful that I may have managed to miss some > crucial document.) > > The task I want to accomplish is very simple: to define and > sequentially initialize M x N variables *programmatically*, according > to two different categories, containing N and M values, respectively. > > In languages with associative arrays, the typical way to do this is to > define a 2-d associative array; e.g. in Perl one could do > > for $i ( 'foo', 'bar', 'baz' ) { > > for $j ( 'eenie', 'meenie', 'minie', 'moe' ) { > > $table{ $i }{ $j } = read_table( "path/to/data/${i}_${j}.dat" ); > } > > } > > How does one do this in R? In particular, what's the equivalent of > the above in R? > > Most importantly, how could I have found out this answer from the > R docs?I'm sure that you will get several answers about the specifics of how to do this. As for where to look, in the "Introduction to R" manual, you will find this section on lists: http://cran.r-project.org/doc/manuals/R-intro.html#Lists-and-data-frames It really helps to sit down with these documents and work your way through them, as you have probably already started to do. Also, read the emails from r-help for a while to learn what is possible to do with R. Sean