[R] Different behaviour of unique(), R vs. Splus.

Rolf Turner rolf at math.unb.ca
Tue Jun 29 17:20:52 CEST 2004

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.'')

	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!


						Rolf Turner

More information about the R-help mailing list