Re: [R] How to detect NULL list element?

From: Wacek Kusnierczyk <Waclaw.Marcin.Kusnierczyk_at_idi.ntnu.no>
Date: Mon, 03 Nov 2008 09:37:59 +0100

Barry Rowlingson wrote:
> 2008/11/3 <rkevinburton@charter.net>:
>
>
>> So how do I detect the NULL at r[1]?
>>
>
> How can you detect what is not there?
>
> Single square brackets on a list give you a list. Double square
> brackets give you the elements.
>
> is.null(r[[1]]) should be TRUE.
>

interestingly:

(l = list(1, NULL))
[[1]]
[1] 1

[[2]]
NULL l = list(1); l[[2]] = NULL; l
[[1]]
[1] 1

l = as.list(1:3); l[[2]] = NULL; l
[[1]]
[1] 1

[[2]]
[1] 2

you can have NULL as an element on a list, but assigning NULL to an element of a list removes the element rather than makes it a NULL.

i find it more coherent if l[i] would remove the element (as it does) while l[[i]] would assign NULL to it (as it doesn't), OR if list(1, NULL) would return a list of 1 element. note, x = NULL *assigns* NULL to x rather than removes x:

x
Error: object "x" not found

x = NULL; x
NULL vQ



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. Received on Mon 03 Nov 2008 - 08:41:09 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 03 Nov 2008 - 14:30:20 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive