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

Gregor Gorjanc gregor.gorjanc at bfro.uni-lj.si
Thu Aug 10 00:26:02 CEST 2006


Prof Brian Ripley wrote:
> 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'.

Thank you for the reply! I appologize for not reading the latest
documentation - there was no word about droping names in 2.3.1. However,
I do wonder why simple fix (as shown in previous mail) is not OK.
Afterall, there is considerable variability on what unique methods drop
and what they preserve. Say:

* factor - keep all levels as in x
* data.frame - keep rownames (which are "needed" by default anyway)
* matrix - keep rownames as well as colnames

and I am perfectly fine with all these, I just wonder why names should
be dropped if x is atomic and not (rownames and colnames) if x is a
matrix, array or data.frame. Did I miss something or does addition of
names back to atomic x break existing code?

R> x <- c(1, 2, 3, 2)
R> names(x) <- 1:length(x)
R> unique(x)
[1] 1 2 3
R> y <- matrix(x)
R> colnames(y) <- 1; rownames(y) <- names(x)
R> t(unique(y))
colnames(y) <- 1; rownames(y) <- names(x)

-- 
Lep pozdrav / With regards,
    Gregor Gorjanc

----------------------------------------------------------------------
University of Ljubljana     PhD student
Biotechnical Faculty
Zootechnical Department     URI: http://www.bfro.uni-lj.si/MR/ggorjan
Groblje 3                   mail: gregor.gorjanc <at> bfro.uni-lj.si

SI-1230 Domzale             tel: +386 (0)1 72 17 861
Slovenia, Europe            fax: +386 (0)1 72 17 888

----------------------------------------------------------------------
"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