[R] no warning with NULL component in data.frame

Thomas Lumley thomas at biostat.washington.edu
Wed May 31 18:30:46 CEST 2000


On Wed, 31 May 2000, Emmanuel Paradis wrote:


> I sent a message a few days ago about NULL components in list or
> data.frame. To reiterate, if I have a list or a data.frame, and try to
> manipulate some elements using the $ symbol, no warning is given if the
> elements do not exist.

"It's not a bug, it's a feature"

You can do is.null(list$component) to test very efficiently whether the
component is there, which would be hard if it gave an error. Also, it
turns out to be useful to be able to handle a list which may have missing
components without a lot of special cases and without errors.

Anyway, it's a fairly fundamental part of the language.

There might be a case for warning if you access a non-existent element of
a data frame. The problem here is that a lot of correct and functional 
code probably does access non-existent elements and the warnings would
get annoying.

Accessing non-existent dataframe columns with "[" does give an error, so
you could do that instead.

>data(trees)
>trees$aardvark
NULL
>trees[,"aardvark"]
Error in [.data.frame(trees, , "aardvark") : 
        subscript out of bounds


	-thomas

Thomas Lumley
Assistant Professor, Biostatistics
University of Washington, Seattle

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list