Skip to content

colnames is slow for data.frames with implicit row.names

3 messages · Brian Ripley, Martin Morgan

#
colnames on a data.frame with implicit row.names
is slow
[1] 21.655  0.327 21.987  0.000  0.000
[1] 0 0 0 0 0

because colnames calls dimnames calls row.names.data.frame calls
as.character on the implicit row.names.
#
On Wed, 27 Sep 2006, Martin Morgan wrote:

            
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.
#
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: