[R] Problem with as.data.frame when an extra attribute is present

Prof Brian Ripley ripley at stats.ox.ac.uk
Fri Nov 10 14:34:49 CET 2006


It's quite intentional, as it is the documented behaviour of data.frame:

      Objects passed to 'data.frame' should have the same number of
      rows, but atomic vectors, factors and character vectors protected
      by 'I' will be recycled a whole number of times if necessary.

> data.frame(a = structure(1, label="foo"), b = c(2, 3))
Error in data.frame(a = structure(1, label = "foo"), b = c(2, 3)) :
         arguments imply differing number of rows: 1, 2

It is safe to replicate a vector without any attributes, but not safe to 
replicate this 'a': you will have to do it yourself if you know it is 
safe.  How is anyone to know you meant 'label' to apply to the whole 
vector and not the single element of the vector (if you did)?


On Thu, 9 Nov 2006, Frank E Harrell Jr wrote:

> I have a problem when one of the vectors in a list needs to be
> replicated to have the appropriate length, and an attribute is present.
>
> > w <- list(a=1, b=2:3)
> > as.data.frame(w)
>   a b
> 1 1 2
> 2 1 3
>
> > attr(w$a,'label') <- 'foo'
> > as.data.frame(w)
> Error in data.frame(a = 1, b = c(2, 3), check.names = TRUE) :
>         arguments imply differing number of rows: 1, 2
>
> I usually use the Hmisc label function to make a variable of class
> 'labelled' and define as.data.frame.labelled as as.data.frame.vector,
> but that also fails here.  Any help appreciated.  -Frank
>
> > sessionInfo()
> R version 2.2.1, 2005-12-20, i486-pc-linux-gnu [also fails in 2.4.0]
>
> attached base packages:
> [1] "methods"   "stats"     "graphics"  "grDevices" "utils"     "datasets"
> [7] "base"
>
>

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



More information about the R-help mailing list