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-bounces at r-project.org [mailto:r-help-bounces at
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 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.