I'm an experienced programmer, but learning R is making me lose the little hair I have left...> list(NULL)[[1]] NULL> length(list(NULL))[1] 1> x <- list() > x[[1]] <- NULL > xlist()> length(x)[1] 0>From the above experiment, it is clear that, although one can create aone-element list consisting of a NULL element, one can't get the same result by assigning NULL to the first element of an empty list. And it gets worse:> x <- list(1, 2, 3) > length(x)[1] 3> x[[2]] <- NULL > length(x)[1] 2 I just could NOT believe my eyes! Am I going crazy??? What I'm trying to do is so simple and straightforward: I want to be able to append NULL to a list, and, after the appending, have the last element of the list be NULL. Is that so unreasonable? How can it be done? TIA! Kynn [[alternative HTML version deleted]]
Continuing your example below, x[4] <- list(NULL) I won't try to defend the semantics,which have been complained about before. However, note that with lists, x[i] is the list which consists of one member, the ith component of x, which is not the same as x[[i]], the ith component, itself; so the assignment above sets the list that contains the 4th component of x to the list that contains NULL, doing what you desire. For similar reasons, x <- c(x,list(NULL)) would also work. I believe all of this is documented in the man page for "[" ; but I grant that it takes some close reading and experimentation to get it. -- Bert Gunter Genentech Nonclinical Statistics -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Kynn Jones Sent: Friday, May 22, 2009 3:52 PM To: r-help at r-project.org Subject: [R] how to insert NULLs in lists? I'm an experienced programmer, but learning R is making me lose the little hair I have left...> list(NULL)[[1]] NULL> length(list(NULL))[1] 1> x <- list() > x[[1]] <- NULL > xlist()> length(x)[1] 0>From the above experiment, it is clear that, although one can create aone-element list consisting of a NULL element, one can't get the same result by assigning NULL to the first element of an empty list. And it gets worse:> x <- list(1, 2, 3) > length(x)[1] 3> x[[2]] <- NULL > length(x)[1] 2 I just could NOT believe my eyes! Am I going crazy??? What I'm trying to do is so simple and straightforward: I want to be able to append NULL to a list, and, after the appending, have the last element of the list be NULL. Is that so unreasonable? How can it be done? TIA! Kynn [[alternative HTML version deleted]] ______________________________________________ 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.
Hi Kynn: this oddity? is discussed in Patrick Burn's document called "The R Inferno". I don't recall the fix so I'm not sure if below is the same as what his book says to do but it seems to do what you want. x <- list() x[[1]] <- 2 x length(x) print(str(x)) x[2] <- list(NULL) x length(x) print(str(x)) Still, I would look at that document rather than just using the above because I'm not an expeRt so there might be a better way ? Also, if you cant find Pat's website, let me know and? I'll find it. I'm pretty sure that,? if you google "Patrick Burns", his site should be up at the top and then his R-Inferno is easy to find from there. It's quite a useful document so I highly recommend it. On May 22, 2009, Kynn Jones <kynnjo at gmail.com> wrote: I'm an experienced programmer, but learning R is making me lose the little hair I have left... > list(NULL) [[1]] NULL > length(list(NULL)) [1] 1 > x <- list() > x[[1]] <- NULL > x list() > length(x) [1] 0 >From the above experiment, it is clear that, although one can create a one-element list consisting of a NULL element, one can't get the same result by assigning NULL to the first element of an empty list. And it gets worse: > x <- list(1, 2, 3) > length(x) [1] 3 > x[[2]] <- NULL > length(x) [1] 2 I just could NOT believe my eyes! Am I going crazy??? What I'm trying to do is so simple and straightforward: I want to be able to append NULL to a list, and, after the appending, have the last element of the list be NULL. Is that so unreasonable? How can it be done? TIA! Kynn [[alternative HTML version deleted]] ______________________________________________ [1]R-help at r-project.org mailing list [2]https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide [3]http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. References 1. mailto:R-help at r-project.org 2. https://stat.ethz.ch/mailman/listinfo/r-help 3. http://www.R-project.org/posting-guide.html