[R] Removing duplicated rows within a matrix, with missing data as wildcards

stacey thompson stacey.lee.thompson at gmail.com
Thu Mar 8 16:14:37 CET 2007


I'd like to remove duplicated rows within a matrix, with missing data
being treated as wildcards.

For example

> x <- matrix((1:3), 5, 3)
> x[4,2] = NA
> x[3,3] = NA
> x

     [,1] [,2] [,3]
[1,]    1    3    2
[2,]    2    1    3
[3,]    3    2   NA
[4,]    1   NA    2
[5,]    2    1    3

I would like to obtain

      [,1] [,2] [,3]
[1,]    1    3    2
[2,]    2    1    3
[3,]    3    2   NA

>From the R-help archives, I learned about unique(x) and duplicated(x).
However, unique(x) returns

> unique(x)

     [,1] [,2] [,3]
[1,]    1    3    2
[2,]    2    1    3
[3,]    3    2   NA
[4,]    1   NA    2

and duplicated(x) gives

> duplicated(x)

[1] FALSE FALSE FALSE FALSE  TRUE

I have tried various na.action 's but with unique(x) I get errors at best.

e.g.
> unique(x, na.omit(x))

Error: argument 'incomparables != FALSE' is not used (yet)

How I might tackle this?

Thanks,

-stacey

-- 
-stacey lee thompson-
Stagiaire post-doctorale
Institut de recherche en biologie végétale
Université de Montréal
4101 Sherbrooke Est
Montréal, Québec H1X 2B2 Canada
stacey.thompson at umontreal.ca



More information about the R-help mailing list