Dear all, 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. > DF <- data.frame(x=c(1,2), y=c(3,4)) > DF x y 1 1 3 2 2 4 > tmp <- c(DF$u, DF$v) > tmp NULL Practically, I met the problem recently reading some data with: DF <- read.table(..., header=TRUE) where the column names were L1, L3, L4, L5, L6 (L2 missing), and then went on concatenating all these: X <- c(DF$L1, DF$L2, DF$L3, DF$L4, DF$L5, DF$L6) which, of course, gave no warning, but then I realized that the length of X was not what I expected. A work-around to this particular problem would be to attach the data.frame: > attach(DF) > tmp <- c(u, v) Error: Object "u" not found But yet, I do not understand the reason for this: > DF$anything.you.could.imagine NULL since I would expect something like: > DF$anything.you.could.imagine Error: Component "anything.you.could.imagine" not found in Object "DF" I suppose this has more to do with the design of the language. I understand too that components within lists or data frames are not objects. Best, Emmanuel Paradis -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
no warning with NULL component in data.frame
2 messages · Emmanuel Paradis, Thomas Lumley
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._