[R] How to create an array of list?

Gang Chen gangchen at mail.nih.gov
Thu Nov 8 23:19:51 CET 2007


Thanks for the response!

I want to create those lists so that I could use them in a function  
('contrast' in contrast package) as arguments.

Any suggestions?

Thanks,
Gang

On Nov 8, 2007, at 5:12 PM, jim holtman wrote:

> Can you tell us what you want to do, and not how you want to do it.
> Without the data it is hard to see.  Some of your indexing probably
> does not have the correct number of parameters when trying to do the
> replacement.  An explanation of what you expect the output to be would
> be useful in determining what the script might look like.
>
> On Nov 8, 2007 4:51 PM, Gang Chen <gangchen at mail.nih.gov> wrote:
>> I have trouble creating an array of lists? For example, I want to do
>> something like this
>>
>> clist <- array(data=NA, dim=c(7, 2, 3));
>> for (n in 1:7) {
>>    for (ii in 1:2) {
>>        for (jj in 1:3) {
>>                if (cc[n, ii, jj] == "0") { clist[n, ii, ][[jj]] <-  
>> list(levels(MyModel[,colnames(MyModel)[jj]])); }
>>           else  { clist[n, ii, ][[jj]] <- cc[n, ii, jj]; }
>>           names(clist[n, ii, ][[jj]]) <- colnames(MyModel)[jj];
>>        }
>>        }
>> }
>>
>> but I get an error:
>>
>> Error in `*tmp*`[n, ii, ] : incorrect number of dimensions
>>
>> Is it because each list has different number of components? The two
>> variables involved in the loop, character matrix cc and dataframe
>> MyModel are shown below:
>>
>>> cc
>> , , 1
>>
>>      [,1] [,2]
>> [1,] "U"  "T"
>> [2,] "0"  "0"
>> [3,] "0"  "0"
>> [4,] "0"  "0"
>> [5,] "U"  "T"
>> [6,] "U"  "T"
>> [7,] "U"  "T"
>>
>> , , 2
>>
>>      [,1] [,2]
>> [1,] "0"  "0"
>> [2,] "M"  "F"
>> [3,] "0"  "0"
>> [4,] "0"  "0"
>> [5,] "0"  "0"
>> [6,] "0"  "0"
>> [7,] "0"  "0"
>>
>> , , 3
>>
>>      [,1] [,2]
>> [1,] "0"  "0"
>> [2,] "0"  "0"
>> [3,] "Lo" "Hi"
>> [4,] "No" "Hi"
>> [5,] "Hi" "Hi"
>> [6,] "Lo" "Lo"
>> [7,] "No" "No"
>>
>>> MyModel
>>    Trust Sex Freq
>> 1      T   F   Hi
>> 2      T   F   Hi
>> 3      T   F   Hi
>> 4      T   F   Hi
>> 5      T   F   Hi
>> 6      T   F   Hi
>> 7      T   F   Hi
>> 8      T   F   Hi
>> 9      T   F   Lo
>> 10     T   F   Lo
>> 11     T   F   Lo
>> 12     T   F   Lo
>> 13     T   F   Lo
>> 14     T   F   Lo
>> 15     T   F   Lo
>> 16     T   F   Lo
>> 17     T   F   No
>> 18     T   F   No
>> 19     T   F   No
>> 20     T   F   No
>> 21     T   F   No
>> 22     T   F   No
>> 23     T   F   No
>> 24     T   F   No
>> 25     T   M   Hi
>> 26     T   M   Hi
>> 27     T   M   Hi
>> 28     T   M   Hi
>> 29     T   M   Hi
>> 30     T   M   Hi
>> 31     T   M   Hi
>> 32     T   M   Hi
>> 33     T   M   Lo
>> 34     T   M   Lo
>> 35     T   M   Lo
>> 36     T   M   Lo
>> 37     T   M   Lo
>> 38     T   M   Lo
>> 39     T   M   Lo
>> 40     T   M   Lo
>> 41     T   M   No
>> 42     T   M   No
>> 43     T   M   No
>> 44     T   M   No
>> 45     T   M   No
>> 46     T   M   No
>> 47     T   M   No
>> 48     T   M   No
>> 49     U   F   Hi
>> 50     U   F   Hi
>> 51     U   F   Hi
>> 52     U   F   Hi
>> 53     U   F   Hi
>> 54     U   F   Hi
>> 55     U   F   Hi
>> 56     U   F   Hi
>> 57     U   F   Lo
>> 58     U   F   Lo
>> 59     U   F   Lo
>> 60     U   F   Lo
>> 61     U   F   Lo
>> 62     U   F   Lo
>> 63     U   F   Lo
>> 64     U   F   Lo
>> 65     U   F   No
>> 66     U   F   No
>> 67     U   F   No
>> 68     U   F   No
>> 69     U   F   No
>> 70     U   F   No
>> 71     U   F   No
>> 72     U   F   No
>> 73     U   M   Hi
>> 74     U   M   Hi
>> 75     U   M   Hi
>> 76     U   M   Hi
>> 77     U   M   Hi
>> 78     U   M   Hi
>> 79     U   M   Hi
>> 80     U   M   Hi
>> 81     U   M   Lo
>> 82     U   M   Lo
>> 83     U   M   Lo
>> 84     U   M   Lo
>> 85     U   M   Lo
>> 86     U   M   Lo
>> 87     U   M   Lo
>> 88     U   M   Lo
>> 89     U   M   No
>> 90     U   M   No
>> 91     U   M   No
>> 92     U   M   No
>> 93     U   M   No
>> 94     U   M   No
>> 95     U   M   No
>> 96     U   M   No
>>
>> Thanks,
>> Gang
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting- 
>> guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
>
>
> -- 
> Jim Holtman
> Cincinnati, OH
> +1 513 646 9390
>
> What is the problem you are trying to solve?



More information about the R-help mailing list