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
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.langfelder at 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 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. >-- Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://www.joshuawiley.com/
Sorry, that was a really half-hearted reply. This will create a new list that is the old list shifted down (and should be much faster than the for loop too). lst <- list(NULL,2) lst2 <- vector("list", length(lst) + 1) lst2[2:length(lst2)] <- lst lst lst2 If you really need to use a for loop, maybe try filling it with NAs instead of NULLs? Josh On Mon, Sep 20, 2010 at 7:26 PM, Joshua Wiley <jwiley.psych at gmail.com> wrote:> 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.langfelder at 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 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. >> > > > > -- > Joshua Wiley > Ph.D. Student, Health Psychology > University of California, Los Angeles > http://www.joshuawiley.com/