Re: [Rd] colnames is slow for data.frames with implicit row.names

From: Prof Brian Ripley <ripley_at_stats.ox.ac.uk>
Date: Wed 27 Sep 2006 - 13:30:51 GMT

On Wed, 27 Sep 2006, Martin Morgan wrote:

> colnames on a data.frame with implicit row.names
>
>> df <- data.frame(x=1:6000000)
>
> is slow
>
>> system.time(colnames(df))
> [1] 21.655 0.327 21.987 0.000 0.000
>> system.time(names(df))
> [1] 0 0 0 0 0
>
> because colnames calls dimnames calls row.names.data.frame calls
> as.character on the implicit row.names.

So use names() and not colnames():

    rownames and colnames for matrices
    row.names and names for data frames.

All colnames assumes is that there is a dimnames method: this could be relevant for objects inheriting from "data.frame", but there is a price for generality.

-- 
Brian D. Ripley,                  ripley@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

______________________________________________
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Received on Wed Sep 27 23:35:17 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 27 Sep 2006 - 14:30:10 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.