[R] counting numbers without replicates in a vector

Liaw, Andy andy_liaw at merck.com
Fri Dec 17 03:21:45 CET 2004



> -----Original Message-----
> From: r-help-bounces at stat.math.ethz.ch 
> [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Peter Dalgaard
> Sent: Thursday, December 16, 2004 5:43 PM
> To: Ray Brownrigg
> Cc: dingjun_cn at yahoo.com; r-help at stat.math.ethz.ch
> Subject: Re: [R] counting numbers without replicates in a vector
> 
> 
> Ray Brownrigg <ray at mcs.vuw.ac.nz> writes:
> 
> > > I am just wondering if there is an easy way to count
> > > in a numeric vector how many numbers don't have
> > > replicates. 
> > > For example, 
> > > a=c(1,1,2,2,3,4,5), how can I know there are three
> > > numbers (3, 4 and 5) without replicates?
> > > 
> > How about:
> > length(table(a)[table(a) == 1])
> 
> Also, probably inefficient, but rather neat:
> 
> > setdiff(a,a[duplicated(a)])
> [1] 3 4 5
 
Probably not (inefficient):

> x <- sample(1:5e4, 2e5, replace=TRUE)
> system.time(ans1 <- which(table(x) == 1), gcFirst=TRUE)
[1] 1.08 0.00 1.10   NA   NA
> system.time(ans2 <- setdiff(x, x[duplicated(x)]), gcFirst=TRUE)
[1] 0.23 0.02 0.26   NA   NA
> setdiff(ans2, as.numeric(names(ans1)))
numeric(0)

Best,
Andy

 
> -- 
>    O__  ---- Peter Dalgaard             Blegdamsvej 3  
>   c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
>  (*) \(*) -- University of Copenhagen   Denmark      Ph: 
> (+45) 35327918
> ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: 
> (+45) 35327907
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! 
> http://www.R-project.org/posting-guide.html
> 
>




More information about the R-help mailing list