Thanks, Bill. match() is nice and efficient. However, I met a problem:

My real data is a large _list_ named "read.genes". I found conflict results
between match() and unique() - the lengths of the outcomes are different
(and my final result are wrong too). I suspect that some different list
components are regarded as the same when they are converted to vectors (the
r-help of match() says "Factors, raw vectors and lists are converted to
character vectors"). Is it possible? And as important, how to fix this?

> read.genes[[1]]
[1] "YAL065C" "YAL063C" "YAR050W" "YHR211W"

> duplicates <- as.vector(table(match(read.genes, read.genes)))

> length(duplicates)
[1] 1424
> read.genes.uniq <- unique(read.genes)
> length(read.genes.uniq)
[1] 1469

> sum(duplicates)
[1] 9945348
> length(read.genes)
[1] 9945348

On Wed, Aug 31, 2011 at 12:42 PM, William Dunlap <wdun...@tibco.com> wrote:

> table(match(x, x)) gives you the numbers but the labels are
> a bit more work.
>
> E.g., I'll define another list
>  > x <- list(c("1", "2", "4"), c("1", "2", "4"), 2^(0:4), 3^(1:2), 2^(0:4))
>  > tb <- table(m <- match(x, x))
>  > m
>  [1] 1 1 3 4 3
>  > tb
>
>  1 3 4
>  2 2 1
> which says that the first element of x is seen twice,
> the third twice, and the fourth once.  How to organize
> that the best depends on what you want to do with the
> data.
>
> Bill Dunlap
> Spotfire, TIBCO Software
> wdunlap tibco.com
>
> > -----Original Message-----
> > From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org]
> On Behalf Of zhenjiang xu
> > Sent: Wednesday, August 31, 2011 9:25 AM
> > To: r-help
> > Subject: [R] counting the duplicates in an object of list
> >
> > Hi all,
> >
> > I have a list x:
> >
> >  > x=list(a=c('1','2'),b=c('2','3'),c=c('1','2'),d=c('2','3'))
> >
> > I can get the unique elements with unique(), but how can I get the
> > number of duplicates for each unique elements?
> >
> > > unique(x)
> > [[1]]
> > [1] "1" "2"
> >
> > [[2]]
> > [1] "2" "3"
> >
> > Thanks
> >
> > --
> > Best,
> > Zhenjiang
> >
> > ______________________________________________
> > R-help@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.
>



-- 
Best,
Zhenjiang

        [[alternative HTML version deleted]]

______________________________________________
R-help@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.

Reply via email to