[Rd] unique.default() drops names (PR#9130)

Gregor.Gorjanc at bfro.uni-lj.si Gregor.Gorjanc at bfro.uni-lj.si
Mon Aug 7 08:36:58 CEST 2006


Hello!

unique on a vector or list drops names, while it does not in case of
data.frames and matrix - rownames and colnames here, but they are 
intuitively the same as names. The following code shows this effect:

vecTest <- c("A", "D", "B", "D", "A")
names(vecTest) <- paste("name", vecTest, sep="")
unique(vecTest)

listTest <- as.list(vecTest)
unique(listTest)

dfTest <- data.frame(vecTest)
unique(dfTest)

matTest <- as.matrix(vecTest)
colnames(matTest) <- "col1"
unique(matTest)

I did not bump into .Internal(unique(x)), but the following change
(diff to latest SVN version) to unique.default seems to work. Is this 
acceptable?

Index: R/src/library/base/R/duplicated.R
===================================================================
--- R/src/library/base/R/duplicated.R   (revision 38809)
+++ R/src/library/base/R/duplicated.R   (working copy)
@@ -38,7 +38,12 @@
 {
     if(!is.logical(incomparables) || incomparables)
        .NotYetUsed("incomparables != FALSE")
+    if(!is.null(names(x))) {
+        namesX <- names(x)
+        isNamed <- TRUE
+    }
     z <- .Internal(unique(x))
+    if(isNamed) names(z) <- namesX[match(z, x)]
     if(is.factor(x))
        factor(z, levels = seq(len=nlevels(x)), labels = levels(x),
                ordered = is.ordered(x))

--please do not edit the information below--

Version:
 platform = i486-pc-linux-gnu
 arch = i486
 os = linux-gnu
 system = i486, linux-gnu
 status = 
 major = 2
 minor = 3.1
 year = 2006
 month = 06
 day = 01
 svn rev = 38247
 language = R
 version.string = Version 2.3.1 (2006-06-01)

Lep pozdrav / With regards,
    Gregor Gorjanc

----------------------------------------------------------------------
University of Ljubljana     PhD student
Biotechnical Faculty        URI: http://www.bfro.uni-lj.si/MR/ggorjan
Zootechnical Department     mail: gregor.gorjanc <at> bfro.uni-lj.si
Groblje 3                   tel: +386 (0)1 72 17 861
SI-1230 Domzale             fax: +386 (0)1 72 17 888
Slovenia, Europe
----------------------------------------------------------------------
"One must learn by doing the thing; for though you think you know it,
 you have no certainty until you try." Sophocles ~ 450 B.C.



More information about the R-devel mailing list