[R] exists function on list objects gives always a FALSE

Wacek Kusnierczyk Waclaw.Marcin.Kusnierczyk at idi.ntnu.no
Tue May 19 18:46:40 CEST 2009


Linlin Yan wrote:
> SmoothData$span is not an object which can be checked by exists(), but
> part of an object which can be checked by is.null().
>
>   

is.null is unhelpful here, in that lists can contain NULL as a named
element, and retrieving a non-existent element returns NULL:

    foo = list(bar=NULL)
    is.null(foo$bar)
    # TRUE
    is.null(foo$foo)
    # TRUE

i must admit i find it surprising that ?'$' does not appropriately
explain what happens if a list is indexed with a name not included in
the list's names.  the closest is

" When extracting, a numerical, logical or character 'NA' index
     picks an unknown element and so returns 'NA' in the corresponding
     element of a logical, integer, numeric, complex or character
     result, and 'NULL' for a list. "

but it's valid for NAs in the index, and

"  If no match is found then 'NULL' is returned. "

but it's in the section on environments.


vQ




More information about the R-help mailing list