In trying to get NULL members into a list, I found out about alist(). x<-alist() x$one<-1 x$two<-NULL but x$two doesn't exist. It seems, though, that an alist is just a list. How can one put NULL members into a list?
On 11/6/2006 3:36 PM, Glen Herrmannsfeldt wrote:> In trying to get NULL members into a list, I found out about alist(). > > x<-alist() > > x$one<-1 > x$two<-NULL > > but x$two doesn't exist. > > It seems, though, that an alist is just a list. > > How can one put NULL members into a list?x <- c(x, list(two=NULL)) Duncan Murdoch
You need to start by first allocating the list. The problem is that you cannot reference a list member that does not exist yet (as in x$two): x <- vector("list", 10) x List x has 10 NULL elements by default. You can then assign values to these as needed. -Christos -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Glen Herrmannsfeldt Sent: Monday, November 06, 2006 3:37 PM To: r-help at stat.math.ethz.ch Subject: [R] alist() In trying to get NULL members into a list, I found out about alist(). x<-alist() x$one<-1 x$two<-NULL but x$two doesn't exist. It seems, though, that an alist is just a list. How can one put NULL members into a list? ______________________________________________ R-help at stat.math.ethz.ch 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.
Glen Herrmannsfeldt <gah4 at u.washington.edu> writes:> In trying to get NULL members into a list, I found out about alist(). > > x<-alist()(that is the same as x <- list(). I don't think alist() is relevant.)> x$one<-1 > x$two<-NULL > > but x$two doesn't exist. > > It seems, though, that an alist is just a list. > > How can one put NULL members into a list?Like this:> x["two"] <- list(NULL) > x$two NULL or just put it there with x <- list(two=NULL) -- O__ ---- Peter Dalgaard ?ster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
Peter Dalgaard <p.dalgaard at biostat.ku.dk> wrote: (regarding adding NULL elements to a list)> > x["two"] <- list(NULL)This seems to work. In the actual case, it is assigning from a variable that may or may not be null, for example: two<-NULL x["two"]<-list(two) It seems to work, and not do what it looks like, add a list of length one. I suppose I should realize by now that scalars are just like lists of length one, but this still wasn't obvious. thanks, -- glen
Peter Dalgaard <p.dalgaard at biostat.ku.dk> wrote:> > > > x["two"] <- list(NULL)> > This seems to work.> > In the actual case, it is assigning from a variable that may > > or may not be null, > > for example:> > two<-NULL > > x["two"]<-list(two)> > It seems to work, and not do what it looks like, add a list of > > length one. I suppose I should realize by now that scalars > > are just like lists of length one, but this still wasn't obvious.> Actually, it is a bit more subtle:> The key point is that the left hand side of the assignment is a list. > This is the difference between indexing with [] rather than [[]] or $.Well, that and the list() on the right: Yes, it is subtle. I tried it out with: x<-list() two<-2 x["two"]<-list(two) typeof(x$two) and it comes out "double", not "list" Even more interesting: x["two"]<-list(two,two) Warning message: number of items to replace is not a multiple of replacement length Instead of a two element list in x["two"] like I might have thought. x[["two"]]<-list(two,two) does generate the list of list form, though. thanks, -- glen