Skip to content

Errors or warnings for non-existent data

2 messages · brwin338, Brian Ripley

#
On 16/03/2013 07:19, brwin338 wrote:
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).