[R] Errors or warnings for non-existent data
Prof Brian Ripley
ripley at stats.ox.ac.uk
Sat Mar 16 08:40:21 CET 2013
On 16/03/2013 07:19, brwin338 wrote:
>
> Good Evening
>
> Is there a setting that I can change in R so that I get either a warning or an error message if I reference non-existent data in a data frame? For example I get no warning or error message with the following:
Use specific methods of indexing as a data frame: [ not $. That $
returns NULL for a non-existent list component is deliberate design, and
as the help says
Note that there is no ‘data.frame’ method for ‘$’, so ‘x$name’
uses the default method which treats ‘x’ as a list. There is a
replacement method which checks ‘value’ for the correct number of
rows, and replicates it if necessary.
E.g.
> df0["w"]
Error in `[.data.frame`(df0, "w") : undefined columns selected
Other bad habits to get out of are not using your space bar, and using =
for assignment in public code: your second line is very hard for humans
to parse compared to
df0 <- data.frame(x = rnorm(1000), y = rnorm(1000))
the cbind() being a waste of time (it forms a matrix and data.frame then
splits it up into columns).
>> set.seed(2013)
>> df0=data.frame(cbind(x=rnorm(1000),y=rnorm(1000)))
>> df0$z=df0$w
>> head(df0)
> x y
> 1 -0.09202453 1.51179695
> 2 0.78901912 -0.60084547
> 3 -0.66744232 -0.02180077
> 4 1.36061149 -0.74909011
> 5 1.50768816 -2.25128946
> 6 -2.60754997 0.43708800
>
> Thanks
> Joe
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
--
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 272866 (PA)
Oxford OX1 3TG, UK Fax: +44 1865 272595
More information about the R-help
mailing list