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

Prof Brian Ripley ripley at stats.ox.ac.uk
Tue Aug 8 09:14:03 CEST 2006


>From the help page (?unique)

 Value:

     For a vector, an object of the same type of 'x', but with only one
     copy of each duplicated element.  No attributes are copied (so the
     result has no names).

Please take your own advice and `learn by doing as you are asked in the 
posting guide'.


On Mon, 7 Aug 2006, Gregor.Gorjanc at bfro.uni-lj.si wrote:

> 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.
> 
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
> 

-- 
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-devel mailing list