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

Prof Brian Ripley ripley at stats.ox.ac.uk
Wed May 31 18:38:13 CEST 2000


> Date: Wed, 31 May 2000 18:07:46 +0200
> To: r-help <r-help at stat.math.ethz.ch>
> From: Emmanuel Paradis <paradis at isem.univ-montp2.fr>
> Subject: [R] no warning with NULL component in data.frame
> 
> 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.

Yes, but did you not want c(DF, recursive=T)?

> 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.  

Yes, it's a feature.  People from Unix backgrounds appreciate getting
sensible answers rather than warnings.  You asked for something that
did not exist, and you got the empty set back.  Makes sense!  It's
up to you to check for it.  After all, if in C you try to open a
non-existent file, you don't get a warning, just a NULL result.

> I understand
> too that components within lists or data frames are not objects.

They _are_ full-status objects.

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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