Hello Peter, This is because assigning a value of NULL removes that element of the list. I am not quite sure what the reference for that is. I remember reading it in the documentation once though. I looked through ?list to no avail. At any rate, to avoid it, you would have to assign something besides NULL.
HTH, Josh On Mon, Sep 20, 2010 at 7:06 PM, Peter Langfelder <peter.langfel...@gmail.com> wrote: > Hello, > > I encountered a weird problem. Consider the following code that takes > a list "lst" and shifts all elements one index up (for example, to > make space for a new first element): > > lst = list(1,2) > ll = length(lst); > for (i in ll:1) > lst[[i+1]] = lst[[i]]; > lst > > If you run it, you get the expected result > > [[1]] > [1] 1 > > [[2]] > [1] 1 > > [[3]] > [1] 2 > > Now I change the input such that the first element is a NULL. > > lst = list(NULL,2) > ll = length(lst); > for (i in ll:1) > lst[[i+1]] = lst[[i]]; > lst > > When you run the code, you get > > [[1]] > NULL > > [[2]] > [1] 2 > > i.e. the shift did not happen. Why is that and how can the shift be > made to work correctly in the presence of NULL elements in the list? > > Thanks, > > Peter > > ______________________________________________ > R-help@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. > -- Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://www.joshuawiley.com/ ______________________________________________ R-help@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.