Problem
I would like to test if an element of a list exists, here is an example
foo <- list(a=1)
exists('foo')
TRUE #foo does exist
exists('foo$a')
FALSE #suggests that foo$a does not exist
foo$a
[1] 1 #but it does exist
In this example, I know that foo$a
exists, but the test returns FALSE
.
I looked in ?exists
and have found that with(foo, exists('a')
returns TRUE
, but do not understand why exists('foo$a')
returns FALSE
.
Questions
- Why does
exists('foo$a')
returnFALSE
? - Is use of
with(...)
the preferred approach?
!is.null(foo$a)
(or!is.null(foo[["a"]])
to be on the safe side) ? (orexists("a",where=foo)
)foo <- list(a1=1)