s-news
[Top] [All Lists]

Re: Unique for matrices

To: Phil Corbett <stpbk@warwick.ac.uk>
Subject: Re: Unique for matrices
From: Anne York <york@noaa.gov>
Date: Tue, 28 Nov 2000 12:02:05 -0800 (PST)
Cc: S News <s-news@wubios.wustl.edu>
In-reply-to: <008c01c0596a$dc19aed0$39f0cd89@SCUPC8>
Reply-to: Anne York <anne.york@noaa.gov>
Here is one  method:
Suppose your matrix is called dum:
1. convert the matrix into a vector of strings:
dum.strings_apply(dum,1,paste,collapse="")

2. find the indices of the first unique values of that vector of
that string. Those will be the row numbers of the unique row of the matrix.

match(unique(dum.strings),dum.strings)

3. output those unique rows:  

 dum[match(unique(dum.strings),dum.strings),]

4. write a function:
"unique.rows" <- function(dum){
dum.strings_apply(dum,1,paste,collapse="")
dum[match(unique(dum.strings),dum.strings),]
}

Here is an example:

> dum
      [,1] [,2] [,3] [,4] 
 [1,]    1    2    3    4
 [2,]    4    3    2    1
 [3,]    1    2    3    4
 [4,]    1    2    3    4
 [5,]    5    4    3    2
 [6,]    4    3    2    1
 [7,]    4    3    2    1
 [8,]    1    2    3    4
 [9,]    5    4    3    2
[10,]    5    4    3    2

 unique.rows(dum)
     [,1] [,2] [,3] [,4] 
[1,]    1    2    3    4
[2,]    4    3    2    1
[3,]    5    4    3    2
> 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Anne E. York
National Marine Mammal Laboratory
Seattle WA 98115-0070  USA
e-mail: anne.york@noaa.gov
Voice: +1 206-526-4039
Fax: +1 206-526-6615
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

On Tue, 28 Nov 2000, Phil Corbett wrote:

>Hi there
>
>Say  I have a matrix with 20 rows and three columns.  This matrix consists
of four  different vectors repeated a number of times (not all the same
frequency).   Is there a function that I can apply to the matrix that will
give me a smaller matrix that consists only of the four different vectors?
>
>Thanks in advance
>
>Phil Corbett 
>


<Prev in Thread] Current Thread [Next in Thread>