Liaw, Andy
2004-Jun-29 17:41 UTC
[R] RE: [S] Different behaviour of unique(), R vs. Splus.
The source of the incompatibility: In S-PLUS 6.2:> methods("unique")splus splus menu splus "unique.data.frame" "unique.default" "unique.name" "unique.rowcol.names" In R-1.9.1:> methods("unique")[1] unique.array unique.data.frame unique.default unique.matrix Unless there's some sort of coordination (or even just separate effort) on either/both R Core and Insightful developers to make sure there's agreement on what methods to provide in the base code, such problem can only get worse, not better, I guess. Best, Andy> From: Rolf Turner > > Apologies for the cross-posting, but I thought this snippet of info > might be vaguely interesting to both lists. > > I did a ***brief*** search to see if this issue had previously been > discussed and found nothing. So I thought I'd tell the list about a > difference in behaviour between unique() in R and unique() in Splus > which bit me just now. > > I was trying to convert a package from Splus to R and got nonsense > answers in R. Turned out that within the bowels of the package I was > doing something like > > u <- unique(y) > > where y was a matrix of integer values. In Splus this gives a > (short) vector of unique values. In R it gives a matrix of the same > dimensionality as y, except that any duplicated rows are eliminated. > > (This looks like being very useful --- once you know about it. And > it was probably mentioned in the R release notes at one time, but, as > Dr. Hook says, ``I was stoned and I missed it.'') > > E.g. > set.seed(42) > m <- matrix(sample(1:5,20,TRUE),5,4) > u <- unique(m) > > In R ``u'' is identical to ``m''; in Splus ``u'' is vector (of > length 5). > > To get what I want in R I simply need to do > > u <- unique(as.vector(y)) > > Simple, once you know. Took me a devil of a long time to track down > what was going wrong, but! > > cheers, > > Rolf Turner > -------------------------------------------------------------------- > This message was distributed by s-news at lists.biostat.wustl.edu. To > ...(s-news.. clipped)...> >
Prof Brian Ripley
2004-Jun-29 20:19 UTC
[R] Re: [S] Different behaviour of unique(), R vs. Splus.
On Tue, 29 Jun 2004, Liaw, Andy wrote:> The source of the incompatibility: > > In S-PLUS 6.2: > > > methods("unique") > splus splus menu splus > "unique.data.frame" "unique.default" "unique.name" "unique.rowcol.names" > > > In R-1.9.1: > > > methods("unique") > [1] unique.array unique.data.frame unique.default unique.matrix > > > Unless there's some sort of coordination (or even just separate effort) on > either/both R Core and Insightful developers to make sure there's agreement > on what methods to provide in the base code, such problem can only get > worse, not better, I guess.There are plans to that effect, but R moves much faster than a commercial product such as S-PLUS. It seems to me a bad idea that unique (or foo) does different things for matrices and data frames, for as we see frequently, many users do not distinguish between them. -- 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