Message-ID: <4A12E270.2060600@idi.ntnu.no>
Date: 2009-05-19T16:46:40Z
From: Wacek Kusnierczyk
Subject: exists function on list objects gives always a FALSE
In-Reply-To: <8d4c23b10905190923v46439b8emc1f9419c7fb5c80@mail.gmail.com>
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