[R] names attribute of data.frames after rbind

Tony Plate tplate at blackmesacapital.com
Mon Apr 26 23:28:53 CEST 2004


AFAIK, data frames don't usually have names attributes on the 
components.  I don't see this explicitly documented, but Venables & Ripley 
MASS seems to imply this in the section on data frames in Chapter 2 (3rd 
ed) (they say "there is a set of names, the row.names, common to all 
variables").  The documentation for "labels" in Hmisc also seems to imply 
that columns of data frames do not have their own names.

So my question would be: is data.frame() correct in not stripping names off 
components?

A couple of simple examples are:
 > data.frame(x=c(a=2,b=2))[[1]]
a b
2 2
 > as.data.frame(list(x=c(a=2,b=2)))[[1]]
a b
2 2
 >

-- Tony Plate

At Monday 12:38 PM 4/26/2004, Angelo Canty wrote:
>Hi,
>
>If columns of a data.frame have a names attribute and we rbind two
>similar data.frames together, the names of the column of the resulting
>data.frame only has the correct values for the first component and
>has "" in all other positions.  Is this a documented "feature" or
>a bug?  If it is intentional, why?  Here is a small example to
>show what I mean.  The same behaviour appears on R1.9.0 for Windows
>and R1.8.1 for Solaris (I haven't got around to upgrading there yet).
>
> > x1 <- 1:5; names(x1)=1:5
> > x2 <- 6:10; names(x2)=6:10
> > x1 <- data.frame(x=x1)
> > x2 <- data.frame(x=x2)
> > x12 <- rbind(x1, x2)
> > attributes(x1$x)
>$names
>[1] "1" "2" "3" "4" "5"
>
> > attributes(x2$x)
>$names
>[1] "6"  "7"  "8"  "9"  "10"
>
> > attributes(x12$x)
>$names
>  [1] "1" "2" "3" "4" "5" ""  ""  ""  ""  ""
>
> > x21 <- rbind(x2, x1)
> > attributes(x21$x)
>$names
>  [1] "6"  "7"  "8"  "9"  "10" ""   ""   ""   ""   ""
>
>
>--
>------------------------------------------------------------------
>|   Angelo J. Canty                Email: cantya at mcmaster.ca     |
>|   Mathematics and Statistics     Phone: (905) 525-9140 x 27079 |
>|   McMaster University            Fax  : (905) 522-0935         |
>|   1280 Main St. W.                                             |
>|   Hamilton ON L8S 4K1                                          |
>
>______________________________________________
>R-help at stat.math.ethz.ch mailing list
>https://www.stat.math.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html




More information about the R-help mailing list