From mtmorgan at fhcrc.org  Wed Sep 27 15:45:32 2006
From: mtmorgan at fhcrc.org (Martin Morgan)
Date: Wed, 27 Sep 2006 06:45:32 -0700
Subject: [Rd] colnames is slow for data.frames with implicit row.names
In-Reply-To: <Pine.LNX.4.64.0609271426220.17192@gannet.stats.ox.ac.uk> (Brian
	Ripley's message of "Wed, 27 Sep 2006 14:30:51 +0100 (BST)")
References: <6phu02tmoyz.fsf@gopher4.fhcrc.org>
	<Pine.LNX.4.64.0609271426220.17192@gannet.stats.ox.ac.uk>
Message-ID: <6phpsdhmnib.fsf@gopher4.fhcrc.org>

Yes, obviously, with hindsight. In lieu of code change, the colnames
documentation could indicate the restricted sense of 'equivalent'.

  For a data frame, 'rownames' and 'colnames' are equivalent to
  'row.names' and 'names' respectively.
     
It might help to add 'names' to See Also of ?data.frame.

Martin

Prof Brian Ripley <ripley at stats.ox.ac.uk> writes:

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

-- 
Martin T. Morgan
Bioconductor / Computational Biology
http://bioconductor.org




[Rd] colnames is slow for data.frames with implicit row.names
Martin Morgan