Re: [Rd] data frame subscription operator

From: Prof Brian Ripley <>
Date: Wed 08 Nov 2006 - 08:21:05 GMT

'[' is the 'subscript' or 'extraction', not 'subscription' operator: this is also called 'indexing', as in 'An Introduction to R'.

On Mon, 6 Nov 2006, Vladimir Dergachev wrote:

> I was looking at the data frame subscription operator (attached in the end
> of this e-mail) and got puzzled by the following line:
> class(x) <- attr(x, "row.names") <- NULL
> This appears to set the class and row.names attributes of the incoming data
> frame to NULL.

Actually no, it removes them: see ?attr and ?class.

> So far I was not able to figure out why this is necessary -
> could anyone help ?

You need to remove the class to avoid recursion: a few lines later x[i] needs to be a call to the primitive and not the data frame method.

> The reason I am looking at it is that changing attributes forces duplication
> of the data frame and this is the largest cause of slowness of data.frames in
> general.

Do you have evidence of that? R has facilities to profile its code, and I have never seen [.data.frame taking a significant proportion of the total time. If it does for your application, consider if a data frame is an appropriate way to store your data. I am not sure we would accept that data frames do have 'slowness in general', but their generality does make them slower than alternatives where the generality is not needed.


Brian D. Ripley,        
Professor of Applied Statistics,
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

______________________________________________ mailing list
Received on Wed Nov 08 19:28:05 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Wed 08 Nov 2006 - 16:30:35 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.