[R] Weird behavior with names()

Prof Brian D Ripley ripley at stats.ox.ac.uk
Thu Jul 15 08:13:30 CEST 1999


On Wed, 14 Jul 1999, Christian Posse wrote:

> Dear R fellows,
> 
> Is the following a feature or a bug of names() ??

It is a feature of matrix indexing.

> It does  not seem to be reported as a bug in the R Bug Tracking System.
> 
> 
> (RedHat 6.0, R-0.64.2 compiled from source, not RPM).
> 
> # a trivial test
> x <- 1:5
> y <- 6:10
> names(x) <- c("a", "b", "c", "d", "e")
> names(y) <- c("a", "b", "c", "d", "e")
>  xy <- cbind(x,y)
> 
> ## test names()
> 
> >  names(x)
> [1] "a" "b" "c" "d" "e"
> >  names(xy)
> NULL
> >  names(xy[,1])
> [1] "a" "b" "c" "d" "e"
> 
> 
> So far, so good.  But now look at what happens when I tried
> to extract from the array:
> 
> >  names(x[-(1:3)])
> [1] "d" "e"
>  >  names(xy[-(1:3),1])                   it works
> [1] "d" "e"
> > names(xy[,1])[-(1:3)]                 it works too.....
> [1] "d" "e"
> 
> >  names(x[-(1:4)])
> [1] "e"
> >  names(xy[-(1:4),1])
> NULL                                                   ??????? should be
> "e"

No, it should not:

> xy[-(1:4),1]
[1] 5

has no names, so names is reporting correctly.  I believe you think it
should have names, but it does not in R nor S. Try (in S)
> xy[-(1:4),1, drop=F]
  x 
e 5

Now, should the name be "e" (xy[, 1][-(1:4)]) or "x" (xy[-(1:4),][1])?
Sensibly, S does not try to choose.

In R, cbind works slightly differently: use xy <- cbind(x=x, y=y)
to see the dilemma more clearly.

[BTW, I think this is a bug in R's cbind and should be fixed.]

-- 
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 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list