Dear R fellows, Is the following a feature or a bug of names() ?? 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"> names(xy[,1])[-(1:4)] That works......[1] "e" Similarly> names(x[5])[1] "e"> names(x[4:5])[1] "d" "e"> names(xy[5,1])NULL ????????> names(xy[4:5,1])[1] "d" "e" Thanks for enlightening me! Christian -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
On Wed, 14 Jul 1999, Christian Posse wrote:> Dear R fellows, > > Is the following a feature or a bug of names() ?? > It does not seem to be reported as a bug in the R Bug Tracking System.Neither. It is a side effect of a feature of matrices. Consider the following three ways to get at the (1,1) element of xy names(xy[,1][1]) #returns name of row 1 names(xy[1,][1]) #returns name of column 1 names(xy[1,1]) #returns NULL What is happening is that xy[1,1] is a 1x1 matrix, which is coerced by default to a scalar, which does not have a names attribute (since it's not clear whether it should be the row 1 or column 1 name) On the other hand xy[,1][1] is computed by coercing xy[,1] to a vector, retaining the row names of xy as the names, and then taking the first element, whose name is the name of the first row of xy. Similarly, names(xy[1,][1]) is the name of the first column of xy. In your example there weren't any column names so it was less obvious. Thomas Lumley Assistant Professor, Biostatistics University of Washington, Seattle -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._