> -----Original Message----- > From: [EMAIL PROTECTED] > [mailto:[EMAIL PROTECTED] On Behalf Of Peter Dalgaard > Sent: Thursday, December 16, 2004 5:43 PM > To: Ray Brownrigg > Cc: [EMAIL PROTECTED]; [EMAIL PROTECTED] > Subject: Re: [R] counting numbers without replicates in a vector > > > Ray Brownrigg <[EMAIL PROTECTED]> 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 > ~~~~~~~~~~ - ([EMAIL PROTECTED]) FAX: > (+45) 35327907 > > ______________________________________________ > [EMAIL PROTECTED] mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > > ______________________________________________ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
