'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")

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.

Reply via email to