[R] loop variable passage and lists

Prof Brian Ripley ripley at stats.ox.ac.uk
Sat Jan 24 14:07:33 CET 2004


On Sat, 24 Jan 2004, Patrick Giraudoux wrote:

> 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?

Note, it says `columns'.  Do all the data frames in your list have 4 
columns?  The message most likely means that one does not.

Hint as requested: using

options(error=dump.frames)
...
debugger()

allows you to debug your code for yourself much more easily than we can
try to debug it.

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595




More information about the R-help mailing list