[R] Count unique rows/columns in a matrix
Gabor Csardi
csardi at rmki.kfki.hu
Fri Jan 11 22:27:50 CET 2008
Hmmm, maybe i wasn't clear enough. So, i need the number
of occurences for each row/column in the matrix. For your
matrix the desired output would be something like a matrix
1 2 3
4 5 6
1 3 2
1 1 1
and the number of occurences for each row:
2 2 1 1
I know some solutions like
https://stat.ethz.ch/pipermail/r-help/2007-December/149033.html
but this is not the best since it converts numeric data to
strings, and this has a lot of problems.
Btw. i've just noticed that unique.matrix does the same.....
I guess i need to code this for myself, just wanted to
be sure that i'm not missing the easy way....
Gabor
On Fri, Jan 11, 2008 at 02:07:37PM -0500, John Kane wrote:
> ?unique & ?length
>
> #unique rows in a matrix
> X<-matrix(c(1,2,3,1,2,3,4,5,6,1,3,2,4,5,6,1,1,1),6,3,byrow=TRUE)
> length(unique(X)[,1])
>
> #unique columns in a data.frame
> Y <- data.frame( a=1:5, b=2:6, c=1:5)
> length(unique(t(Y)[,1]))
>
> --- Gabor Csardi <csardi at rmki.kfki.hu> wrote:
>
> > Dear List,
> >
> > i know there are some solutions for this in the
> > archive,
> > but they're not very good for numeric matrices,
> > since they
> > usually convert rows/columns to character strings.
> > Is there
> > an easy way to do $subject for numeric matrices
> > properly,
> > or i need to do it by hand?
> >
> > Thanks,
> > Gabor
> >
> > ______________________________________________
> > R-help at r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide
> > http://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained,
> > reproducible code.
> >
>
>
>
> Get a sneak peak at messages with a handy reading pane with All new Yahoo! Mail: http://mail.yahoo.ca
--
Csardi Gabor <csardi at rmki.kfki.hu> UNIL DGM
More information about the R-help
mailing list