[R] using a list to index elements of a list

Benilton Carvalho beniltoncarvalho at gmail.com
Tue Mar 23 16:44:56 CET 2010


can you also post an example of A and an example of the expected result?

On Tue, Mar 23, 2010 at 3:36 PM, Pj253 <pj253 at cam.ac.uk> wrote:
>
> Thanks for your reply Ben!
>
> I don't think I want v to be identical to x... I guess I haven't put the
> question in the right context. What I'm actually trying to do is... (*
> indicates extra information, not necessarily relevant for my question, but
> to help put it in context)
>
> A, some  matrix (*a traceless, symmetric matrix of 0's, 1's (representing a
> graph)*)
>
> x<-list()
> x[[1]]<-1:nrow(A)
> for (i in x[[1]]){
>        if (A[1,i]==1) {
>            x[[2]]<-x[[1]][!(x[[1]]==1)]
>            for (j in x[[2]]){
>                    if (A[i,j]==1){
>                        path<-c(1,i,j)
>                        print(path)
>                        }
>                    }
>        }
> }
>
> So, here I have used i and j to range over the elements of x[[1]], x[[2]]
> respectively.
> (*This prints all the paths of length 2 starting at vertex 1. But I'm trying
> to generalise this for a path of length nrow(A), i.e. a Hamiltonian path)*)
> I want to iterate this process a certain number of times, so instead of i,j
> I thought to use a list of scalars, v.
> I don't want v to be identical to x... I want v to be a list of scalars
> which range (inside a for loop) over an elements of the list x. (so instead
> of for (i in x[[k]]) I have for (v[[k]] in x[[k]])).
>
> I can see how they look similar, but I do think they're different objects.
> Hope this is clear. If you still think using 'x' itself would work, can you
> explain how so?
> --
> View this message in context: http://n4.nabble.com/using-a-list-to-index-elements-of-a-list-tp1679184p1679253.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.
>



More information about the R-help mailing list