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

Pj253 pj253 at cam.ac.uk
Tue Mar 23 15:53:45 CET 2010


I have a list of vectors, x, with x[[1]]=1:5, say.

And I need to go through each element of each vector in a for loop.
Something like:

for (v in x[[1]])
print(v)

However, I need to store this index "v" for later, and I have lots of other
indices which we range over later in the code so thought I'd make a list of
these indices, v<-list(). But then when I try:

for( v[[1]] in x[[1]] )
print(v[[1]])

I get errors:

> x<-list()
> x[[1]]=1:5
> v<-list()
> for(v[[1]] in x[[1]])
Error: unexpected '[[' in "for(v[["
> print v[[1]]
Error: unexpected symbol in "print v"

Can you not use a list in this way, i.e. to store variables to range over in
a for loop? Can anyone offer a solution?

Thanks for any help!

-- 
View this message in context: http://n4.nabble.com/using-a-list-to-index-elements-of-a-list-tp1679184p1679184.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list