[R] Anything like associative arrays in R?
James Reilly
reilly at stat.auckland.ac.nz
Wed Nov 2 15:17:57 CET 2005
Also, if you want to read in various data files as in your Perl example,
you could replace the line inside both loops with something like this:
x[[i]][[j]] <- read.table(paste('/path/to/data/',i,'_',j,'.dat',sep=''))
See http://cran.r-project.org/doc/manuals/R-intro.html#Character-vectors
for another paste() function example.
On 3/11/2005 2:30 a.m., jim holtman wrote:
> 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 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?
>>
>>Many thanks in advance,
>>
>>kj
>>
>>______________________________________________
>>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
>>
>
>
>
>
> --
> Jim Holtman
> Cincinnati, OH
> +1 513 247 0281
>
> What the problem you are trying to solve?
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> 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
--
James Reilly
Department of Statistics, University of Auckland
Private Bag 92019, Auckland, New Zealand
More information about the R-help
mailing list