On 25-Oct-09 09:52:42, Patrick Burns wrote: > 'The R Inferno' page 59. > > Patrick Burns > patr...@burns-stat.com > +44 (0)20 8525 0696 > http://www.burns-stat.com > (home of "The R Inferno" and "A Guide for the Unwilling S User")
Which essentially says that If you want the component [x1[comp] of the list x1] to stay there but to be NULL, then do: xl[comp] <- list(NULL) I agree that this can be very puzzling! The essential point is that (moving to Maura's example) myList[2] or, equivalently, myList["second"] is a LIST (whose only component is that component of the original myList). On the other hand, myList[[2]] or, equivalently, myList$second is NOT a list, but is the value of that component of myList: myList[1] # $first # [1] "aaa" myList[[1]] # [1] "aaa" myList["first"] # $first # [1] "aaa" myList[["first"]] # [1] "aaa" Note the statement (under "Recursive (list-like) objects") in ?"$" or, equivalently, ?Extract When either '[[' or '$' is used for replacement, a value of 'NULL' deletes the corresponding item of the list. Therefore changing the value of a comnponent of a list to NULL deletes it. So you have to work at the list level, replacing one list by another list. Hence Patrick's tip. Ted. > mau...@alice.it wrote: >> I can define a list containing NULL elements: >> >>> myList <- list("aaa",NULL,TRUE) >>> names(myList) <- c("first","second","third") >>> myList >> $first >> [1] "aaa" >> $second >> NULL >> $third >> [1] TRUE >>> length(myList) >> [1] 3 >> >> However, if I assign NULL to any of the list element then such >> element is deleted from the list: >> >>> myList$second <- NULL >>> myList >> $first >> [1] "aaa" >> $third >> [1] TRUE >>> length(myList) >> [1] 2 >>> # >>> myList$first <- NULL >>> myList >> $third >> [1] TRUE >>> length(myList) >> [1] 1 >> >> Instead vectors cannot include NULL element: >> >>> vec <- c(TRUE,NULL,FALSE) >>> vec >> [1] TRUE FALSE >>> length(vec) >> [1] 2 >>> vec[1] <- NULL >> Error in vec[1] <- NULL : replacement has length zero >> >> Is the above shown behaviour of list data structures to be expected ? >> I took me a lot of sweat to figure out this wierd behaviour was the >> cause of a bug >> in my big program. >> In general, if I have a list with some elements initialized to NULL, >> that can be changed >> dynamically, then how can I reinitialize such elements to NULL without >> deleting them >> from the list ? >> >> Thank you in advance, >> Maura >> >> >> >> >> >> [[alternative HTML version deleted]] >> >> ______________________________________________ >> 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. >> > > ______________________________________________ > 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. -------------------------------------------------------------------- E-Mail: (Ted Harding) <ted.hard...@manchester.ac.uk> Fax-to-email: +44 (0)870 094 0861 Date: 25-Oct-09 Time: 09:30:45 ------------------------------ XFMail ------------------------------ ______________________________________________ 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.