[R] How to create an array of list?
jim holtman
jholtman at gmail.com
Fri Nov 9 01:46:29 CET 2007
I think something like this is what you are after. This will create 7
pairs of lists with the parameters that I think you want. I don't
have the data (if you want to sent it to me, I may be able to test it)
so you will have to test it yourself.
# create a list for the results
result <- vector('list' 7)
for (n in 1:7){
# initialize the pair of list in the result
result[[n]] <- vector('list', 2)
for (ii in 1:2){
sublist <- vector('list', 3) # for the parameters
for (jj in 1:3){
if(cc[n, ii, jj] == "0") sublist[[jj]] <- levels(MyModel[, jj])
else sublist[[jj]] <- cc[n, ii, jj]
}
names(sublist) <- names(MyModel)
result[[n]][[ii]] <- sublist
}
}
str(result) # see what it looks like
On Nov 8, 2007 6:31 PM, Gang Chen <gangchen at mail.nih.gov> wrote:
> Thanks again for the response!
>
> For example, I want to run the following
>
> > contrast(fit.lme, list(Trust="U", Sex=levels(Model$Sex),
> Freq=levels(Model$Freq)), list(Trust="T", Sex=levels(Model$Sex),
> Freq=levels(Model$Freq)))
>
> The 2nd and 3rd arguments are two lists that I'm trying to construct
> based on the data frame 'Model'. Of course I could provide the two
> lists explicitly as the above command. However for a general usage, I
> would like to build the two lists from the user's input. That is how
> the issue of creating an array of list came about. In the example I
> provided, it would run 7 separate contrasts line the one shown above,
> each of which contains 2 lists, and each list has 3 named components
> (Trust, Sex, and Freq) each of which is of unequal components
> (depending on the contrast specification). And that is why I wanted
> to have an array of 7 X 2 X 3.
>
> Hope this is clearer. Any better solutions?
>
> Thanks,
> Gang
>
>
>
>
> On Nov 8, 2007, at 6:18 PM, jim holtman wrote:
>
> > I am still not sure what you expect as output. Can you provide an
> > example of what you think that you need. What is it that you are
> > trying to construct? How do you then plan to use them? There might
> > be other ways of going about it if we knew what the intent was -- what
> > is the structure that you are trying to create? The code that you
> > have is probably having problems with the number of elements in the
> > replacement, so to see what the alternatives are, can you give an
> > explicit example of what you would like as an outcome and then how you
> > intend to use it.
> >
> > On Nov 8, 2007 5:19 PM, Gang Chen <gangchen at mail.nih.gov> wrote:
> >> 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?
> >>
> >>
> >
> >
> >
> > --
> > Jim Holtman
> > Cincinnati, OH
> > +1 513 646 9390
> >
> > What is the problem you are trying to solve?
>
>
--
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