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

Wacek Kusnierczyk Waclaw.Marcin.Kusnierczyk at idi.ntnu.no
Tue May 19 21:45:25 CEST 2009


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




More information about the R-help mailing list