Hi, 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? Thank you! Jun =====
Sundar Dorai-Raj
2004-Dec-16 21:50 UTC
[R] counting numbers without replicates in a vector
Jun Ding wrote:> Hi, > 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 using ?table: tab <- table(a) unq <- names(tab)[tab == 1] Then use as.numeric(unq) to convert the names to numbers if needed. --sundar
Have you considered something like the following: > a [1] 1 1 2 2 3 4 5 > sum(table(a)==1) [1] 3 hope this helps. spencer graves Jun Ding wrote:>Hi, >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? > >Thank you! > >Jun > > >====> >______________________________________________ >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 > >-- Spencer Graves, PhD, Senior Development Engineer O: (408)938-4420; mobile: (408)655-4567
On 12/16/04 13:40, Jun Ding wrote: Hi, 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? Take a look at unique() -- Jonathan Baron, Professor of Psychology, University of Pennsylvania Home page: http://www.sas.upenn.edu/~baron R search page: http://finzi.psych.upenn.edu/
?duplicated and ?unique might be of interest to you ... But I think an easier way is: z<-table(a) length(z)-sum(z>1) This gives you the count. names(z)[z==1] gives you the actual values (The quotes can be removed by explicitly calling print with argument quote=FALSE) -- Bert Gunter Genentech Non-Clinical Statistics South San Francisco, CA "The business of the statistician is to catalyze the scientific learning process." - George E. P. Box> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Jun Ding > Sent: Thursday, December 16, 2004 1:41 PM > To: r-help at stat.math.ethz.ch > Subject: [R] counting numbers without replicates in a vector > > Hi, > 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? > > Thank you! > > Jun > > > ====> > ______________________________________________ > 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 >
table will probably do. The following is probably is what you want: > a <- c(1,1,2,2,3,4,5) > a.tab <- table (a) > a.tab a 1 2 3 4 5 2 2 1 1 1 > as.vector(which (a.tab == 1)) [1] 3 4 5 Cheers, Yu Jun Ding wrote:>Hi, >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? > >Thank you! > >Jun > > >====> >______________________________________________ >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 > > > >
> 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]) ? Ray Brownrigg
On Thu, 2004-12-16 at 13:40 -0800, Jun Ding wrote:> Hi, > 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? > > Thank you! > > JunHow about:> as.vector(which(table(a) == 1))[1] 3 4 5 HTH, Marc Schwartz
> -----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 5Probably 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 > >