An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20130807/6f6f5461/attachment.pl>
Extracting only multiple occurrences
10 messages · David Winsemius, Berend Hasselman, Rolf Turner +2 more
On Aug 7, 2013, at 10:37 PM, Kevin Parent wrote:
Hoping someone here can help me with this small problem. set.seed(2013) x<-sort(c(letters,letters[sample(26,10,1)])) This gives a vector of 36 letters with some muliples (in this case, g,m,s,t,u,v,x,y). Now what I need is to get rid of the ones that only occur once and keep the multiples. I need the opposite of the unique() function. I expect this should be pretty easy but I can't see it. Anyone know a solution? Thanks in advance!
?duplicated x[ duplicated(x) ] David Winsemius Alameda, CA, USA
On Aug 7, 2013, at 11:03 PM, David Winsemius wrote:
On Aug 7, 2013, at 10:37 PM, Kevin Parent wrote:
Hoping someone here can help me with this small problem. set.seed(2013) x<-sort(c(letters,letters[sample(26,10,1)])) This gives a vector of 36 letters with some muliples (in this case, g,m,s,t,u,v,x,y). Now what I need is to get rid of the ones that only occur once and keep the multiples. I need the opposite of the unique() function. I expect this should be pretty easy but I can't see it. Anyone know a solution? Thanks in advance!
?duplicated x[ duplicated(x) ]
Also this may be of interest: cran.r-project.org/doc/contrib/Short-refcard.pdf? ... but I was suprised to find that both duplicated and rle are absent from Short's refcards.
David Winsemius Alameda, CA, USA
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20130807/8a24fe11/attachment.pl>
On Aug 7, 2013, at 11:23 PM, Kevin Parent wrote:
Well that almost works, and I didn't know about duplicated() so thanks for that. However, it only gives me the duplicated values. I need the original ones too. So the result I want is: [g,g,m,m,s,s,t,t,u,u,u,v,v,x,x,y,y,y]. What duplicated() gives me is [g,m,s,t,u,u,v,x,y,y]
x[ duplicated(x) | duplicated(x, fromLast=TRUE) ]
David. > > Playing around with it, I got this but can't helping thinking there must be a less awkward way: > set.seed(2013) > x<-sort(c(letters,letters[sample(26,10,1)])) > x<-x[duplicated(x)] > x<-sort(c(x,unique(x))) > _____ > Kevin Parent, Ph.D > Korea Maritime University > From: David Winsemius <dwinsemius at comcast.net> > To: Kevin Parent <ksparent at yahoo.com> > Cc: "r-help at r-project.org" <r-help at r-project.org> > Sent: Thursday, August 8, 2013 3:03 PM > Subject: Re: [R] Extracting only multiple occurrences > > > On Aug 7, 2013, at 10:37 PM, Kevin Parent wrote: > > > Hoping someone here can help me with this small problem. > > set.seed(2013) > > > > x<-sort(c(letters,letters[sample(26,10,1)])) > > > > This gives a vector of 36 letters with some muliples (in this case, g,m,s,t,u,v,x,y). Now what I need is to get rid of the ones that only occur once and keep the multiples. I need the opposite of the unique() function. I expect this should be pretty easy but I can't see it. Anyone know a solution? Thanks in advance! > > > > > ?duplicated > > x[ duplicated(x) ] > > > David Winsemius > Alameda, CA, USA > > > David Winsemius Alameda, CA, USA
On 08/08/2013 04:23 PM, Kevin Parent wrote:
Well that almost works, and I didn't know about duplicated() so thanks for that. However, it only gives me the duplicated values. I need the original ones too. So the result I want is: [g,g,m,m,s,s,t,t,u,u,u,v,v,x,x,y,y,y]. What duplicated() gives me is [g,m,s,t,u,u,v,x,y,y]
Hi Kevin, How about: x[x %in% duplicated(x)] Jim
On 08-08-2013, at 10:27, Jim Lemon <jim at bitwrit.com.au> wrote:
On 08/08/2013 04:23 PM, Kevin Parent wrote:
Well that almost works, and I didn't know about duplicated() so thanks for that. However, it only gives me the duplicated values. I need the original ones too. So the result I want is: [g,g,m,m,s,s,t,t,u,u,u,v,v,x,x,y,y,y]. What duplicated() gives me is [g,m,s,t,u,u,v,x,y,y]
Hi Kevin, How about: x[x %in% duplicated(x)]
Don't you mean this x[x %in% x[duplicated(x)]] Berend
On 08/08/13 20:27, Jim Lemon wrote:
On 08/08/2013 04:23 PM, Kevin Parent wrote:
Well that almost works, and I didn't know about duplicated() so thanks for that. However, it only gives me the duplicated values. I need the original ones too. So the result I want is: [g,g,m,m,s,s,t,t,u,u,u,v,v,x,x,y,y,y]. What duplicated() gives me is [g,m,s,t,u,u,v,x,y,y]
Hi Kevin, How about: x[x %in% duplicated(x)]
Uh, I think you mean
x[x %in% x[duplicated(x)]]
Another idear:
tx <- table(x)
tx <- tx[tx>1]
rep(names(tx),tx)
Well, that's three lines as opposed to one, so not as good. But it
perhaps demonstrates
a useful tool to add to one's kit.
cheers,
Rolf
On 08/08/2013 06:52 PM, Berend Hasselman wrote:
On 08-08-2013, at 10:27, Jim Lemon<jim at bitwrit.com.au> wrote:
On 08/08/2013 04:23 PM, Kevin Parent wrote:
Well that almost works, and I didn't know about duplicated() so thanks for that. However, it only gives me the duplicated values. I need the original ones too. So the result I want is: [g,g,m,m,s,s,t,t,u,u,u,v,v,x,x,y,y,y]. What duplicated() gives me is [g,m,s,t,u,u,v,x,y,y]
Hi Kevin, How about: x[x %in% duplicated(x)]
Don't you mean this x[x %in% x[duplicated(x)]] Berend
Ah, yes, thanks. Jim
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20130808/99bec511/attachment.pl>