[R] loop variable passage and lists

Patrick Giraudoux patrick.giraudoux at univ-fcomte.fr
Sat Jan 24 13:36:29 CET 2004

I cannot understand why the following expression is accepted (and gives the expected result: to set column 3 and 4 of the first
element of list1 -a data.frame list- as first element of list2):

> list2[[1]]<-list1[[1]][3:4]

...while this one is not (to do the same iteratively from the first to the eleventh element of list1):

> for (i in 1:11){list2[[i]]<-list1[[i]][3:4]}
Error in "[.data.frame"(list1[[i]], 3:4) :
        undefined columns selected

It looks like if the loop variable "i" could not be passed into the loop.

Any hint?

Patrick Giraudoux

More information about the R-help mailing list