Hi,> x=c(rep(1,3),rep(3,2)) > x[1] 1 1 1 3 3> duplicated(x)[1] FALSE TRUE TRUE FALSE TRUE>As shown in the above code, 'duplicated' doesn't return 'F' for the first '1' and first '3' in 'x'. I am wondering if there is a function that can return an indicator for any element whether it appears in a vector twice or more. Regards, Peng
Try this:> x <- c(1, 2, 3, 3, 4, 5)> ave(x, x, FUN = length) > 1[1] FALSE FALSE TRUE TRUE FALSE FALSE On Thu, Oct 1, 2009 at 10:42 PM, Peng Yu <pengyu.ut at gmail.com> wrote:> Hi, > >> x=c(rep(1,3),rep(3,2)) >> x > [1] 1 1 1 3 3 >> duplicated(x) > [1] FALSE ?TRUE ?TRUE FALSE ?TRUE >> > > As shown in the above code, 'duplicated' doesn't return 'F' for the > first '1' and first '3' in 'x'. I am wondering if there is a function > that can return an indicator for any element whether it appears in a > vector twice or more. > > Regards, > Peng > > ______________________________________________ > 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. >
Bill Dunlap TIBCO Software Inc - Spotfire Division wdunlap tibco.com> -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of Peng Yu > Sent: Thursday, October 01, 2009 7:42 PM > To: r-help at stat.math.ethz.ch > Subject: [R] How to get duplicated items in a vector? > > Hi, > > > x=c(rep(1,3),rep(3,2)) > > x > [1] 1 1 1 3 3 > > duplicated(x) > [1] FALSE TRUE TRUE FALSE TRUE > > > > As shown in the above code, 'duplicated' doesn't return 'F' for the > first '1' and first '3' in 'x'. I am wondering if there is a function > that can return an indicator for any element whether it appears in a > vector twice or more.You could use is.element() or %in%, as in > x <- rep(3:1, 3:1) > x %in% x[duplicated(x)] [1] TRUE TRUE TRUE TRUE TRUE FALSE Bill Dunlap TIBCO Software Inc - Spotfire Division wdunlap tibco.com> > Regards, > Peng > > ______________________________________________ > 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. >
Try this:> x=c(rep(1,3),rep(3,2)) > x[1] 1 1 1 3 3> duplicated(x) | duplicated(x, fromLast=TRUE)[1] TRUE TRUE TRUE TRUE TRUE>On Thu, Oct 1, 2009 at 10:42 PM, Peng Yu <pengyu.ut at gmail.com> wrote:> Hi, > >> x=c(rep(1,3),rep(3,2)) >> x > [1] 1 1 1 3 3 >> duplicated(x) > [1] FALSE ?TRUE ?TRUE FALSE ?TRUE >> > > As shown in the above code, 'duplicated' doesn't return 'F' for the > first '1' and first '3' in 'x'. I am wondering if there is a function > that can return an indicator for any element whether it appears in a > vector twice or more. > > Regards, > Peng > > ______________________________________________ > 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?
Reasonably Related Threads
- How to figure out which the version of split is used?
- How x[, 'colname1'] is implemented?
- How x[, 'colname1'] is implemented?
- Why I get this error? Error in close.connection(f) : invalid connection
- Is there a summary on different version of 'apply' functions? What is the meaning of the prefixes?