An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20090519/2cd543bc/attachment-0001.pl>
exists function on list objects gives always a FALSE
8 messages · Žroutík, Linlin Yan, Duncan Murdoch +3 more
SmoothData$span is not an object which can be checked by exists(), but part of an object which can be checked by is.null().
On Wed, May 20, 2009 at 12:07 AM, ?rout?k <zroutik at gmail.com> wrote:
Dear R-users, in a minimal example exists() gives FALSE on an object which obviously does exist. How can I check on that list object anyway else, please?
SmoothData <- list(exists=TRUE, span=0.001) SmoothData
$exists [1] TRUE $span [1] 0.001
exists("SmoothData")
TRUE
exists("SmoothData$span")
FALSE
exists("SmoothData[[2]]")
FALSE Thank you for any opinion regarding this topic. Zroutik ? ? ? ?[[alternative HTML version deleted]]
______________________________________________ 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.
?rout?k wrote:
SmoothData <- list(exists=TRUE, span=0.001)
SmoothData
$exists [1] TRUE $span [1] 0.001
exists("SmoothData")
TRUE
exists("SmoothData$span")
FALSE
'SmoothData$span' = 'foo'
exists("SmoothData$span")
# TRUE
exists("SmoothData[[2]]")
'SmoothData[[2]]' = 'bar'
exists("SmoothData[[2]]")
# TRUE
the problem in your case is that you have an object named 'SmoothData'
with a nested component named 'span', but you're testing for the
existence of an object named 'SmoothData$span'.
as shown in a recent post, one attempt to do your task would be
exists('SmoothData') && 'span' %in% names(SmoothData)
# TRUE
vQ
On 5/19/2009 12:07 PM, ?rout?k wrote:
Dear R-users, in a minimal example exists() gives FALSE on an object which obviously does exist. How can I check on that list object anyway else, please?
SmoothData <- list(exists=TRUE, span=0.001) SmoothData
$exists [1] TRUE $span [1] 0.001
exists("SmoothData")
TRUE
exists("SmoothData$span")
FALSE
exists("SmoothData[[2]]")
FALSE Thank you for any opinion regarding this topic.
There is no variable with name "SmoothData$span", there is an element of SmoothData with name "span". To test for that, the safest test is probably "span" %in% names(SmoothData) but a common convention is to use is.null(SmoothData$span) because NULL elements are rare in lists. Duncan Murdoch
?rout?k wrote:
Dear R-users, in a minimal example exists() gives FALSE on an object which obviously does exist. How can I check on that list object anyway else, please?
SmoothData <- list(exists=TRUE, span=0.001)
SmoothData
$exists [1] TRUE $span [1] 0.001
exists("SmoothData")
TRUE
exists("SmoothData$span")
FALSE
This checks for existance of an object called "SmoothData$span", as in :
`SmoothData$span` <- 1:10
exists("SmoothData$span")
You can do:
is.list( SmoothData ) && !is.null(names(SmoothData)) && "span" %in%
names(SmoothData)
exists("SmoothData[[2]]")
FALSE
Similarly:
`SmoothData[[2]]` <- 1
exists("SmoothData[[2]]")
You can do:
is.list( SmoothData ) && length(SmoothData) > 1
Thank you for any opinion regarding this topic. Zroutik
Romain Francois Independent R Consultant +33(0) 6 28 91 30 30 http://romainfrancois.blog.free.fr
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
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20090519/226f17a8/attachment-0001.pl>
Stavros Macrakis wrote:
You might think that you can check names(xxx) to see if the slot has been explicitly set, but it depends on *how* you have explicitly set the slot to NULL:
> xxx$hello <- 3 > xxx$hello <- NULL > names(xxx)
character(0) # no names -- assigning to NULL kills slot
kills indeed:
foo = list(bar=1)
with(foo, bar)
# 1
foo$bar = NULL
with(foo, bar)
# error: object 'bar' not found
> xxx <- list(hello=NULL) > names(xxx)
[1] "hello" # 1 name -- constructing with NULL-valued slot
but:
# cleanup -- don't do it in mission critical session
rm(list=ls())
foo
# error: object 'foo' not found
foo = NULL
foo
# NULL
that is, foo$bar = NULL kills bar within foo (even though NULL is a
valid component of lists), but foo = NULL does *not* kill foo.
Welcome to R!
... and its zemanticks. vQ