Re: [R] counting numbers without replicates in a vector
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 __ [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
Re: [R] counting numbers without replicates in a vector
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! Jun How about: as.vector(which(table(a) == 1)) [1] 3 4 5 HTH, Marc Schwartz __ [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
RE: [R] counting numbers without replicates in a vector
-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
[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 = __ [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
Re: [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 __ [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
Re: [R] counting numbers without replicates in a vector
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/ __ [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
RE: [R] counting numbers without replicates in a vector
?duplicated and ?unique might be of interest to you ... But I think an easier way is: z-table(a) length(z)-sum(z1) 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: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Jun Ding Sent: Thursday, December 16, 2004 1:41 PM To: [EMAIL PROTECTED] 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 = __ [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
Re: [R] counting numbers without replicates in a vector
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 = __ [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