# [R] How to detect NULL list element?

rkevinburton at charter.net rkevinburton at charter.net
Mon Nov 3 18:20:05 CET 2008

```Thank you for the input.

The FAQ indicates:

How can I set components of a list to NULL?

I wanted to detect if an element is NULL or missing. I have come up with a work-around that seems to work. Maybe others will have a better way:

if(length(rlist) < index  || is.null(rlist[[index]]))

I knew that [[]] gave me the element at the specified index but I kept running into a error that the index was out of bounds. So, I put in the first test. So if I am testing for the 4th elemant and only element 3 has been set this will detext that the list is not that long and considers it a NULL element.

Kevin

---- Wacek Kusnierczyk <Waclaw.Marcin.Kusnierczyk at idi.ntnu.no> wrote:
> Erik Iverson wrote:
> > FYI, this is FAQ 7.1
>
> ... which says what you should and should not do, but without any a
> justification for the design.  coherently, x[i] = list(NULL) should do
> the same as x[[i]] = NULL, if only for symmetry with x[i] = list(1) and
> x[[1]] = 1, for example.
>
> vQ
>
> >
> > Wacek Kusnierczyk wrote:
> >> Barry Rowlingson wrote:
> >>> 2008/11/3  <rkevinburton at 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.
>
>
> --
> -------------------------------------------------------------------------------
> Wacek Kusnierczyk, MD PhD
>
> Email: waku at idi.ntnu.no
> Phone: +47 73591875, +47 72574609
>
> Department of Computer and Information Science (IDI)
> Faculty of Information Technology, Mathematics and Electrical Engineering (IME)
> Norwegian University of Science and Technology (NTNU)
> Sem Saelands vei 7, 7491 Trondheim, Norway
> Room itv303
>
> Bioinformatics & Gene Regulation Group
> Department of Cancer Research and Molecular Medicine (IKM)
> Faculty of Medicine (DMF)
> Norwegian University of Science and Technology (NTNU)
> Laboratory Center, Erling Skjalgsons gt. 1, 7030 Trondheim, Norway
> Room 231.05.060
>
> ______________________________________________
> 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.

```