Can anyone tell me how to remove several numbers for a sequence. For example: xx<- c(1,5,7,10) yy<-seq(1,10,1) how do I get take xx away from yy to get the new sequence 2,3,4,6,8,9 Many thanks in advance -- View this message in context: http://r.789695.n4.nabble.com/Remove-several-numbers-from-a-sequence-tp4640630.html Sent from the R help mailing list archive at Nabble.com.
R. Michael Weylandt <michael.weylandt@gmail.com>
2012-Aug-17 16:41 UTC
[R] Remove several numbers from a sequence
Take a look at ?setdiff Michael On Aug 17, 2012, at 12:18 PM, penguins <catrsw at bas.ac.uk> wrote:> Can anyone tell me how to remove several numbers for a sequence. For example: > > xx<- c(1,5,7,10) > yy<-seq(1,10,1) > > how do I get take xx away from yy to get the new sequence > > 2,3,4,6,8,9 > > Many thanks in advance > > > > -- > View this message in context: http://r.789695.n4.nabble.com/Remove-several-numbers-from-a-sequence-tp4640630.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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.
yy[!yy%in%xx] Clint Bowman INTERNET: clint at ecy.wa.gov Air Quality Modeler INTERNET: clint at math.utah.edu Department of Ecology VOICE: (360) 407-6815 PO Box 47600 FAX: (360) 407-7534 Olympia, WA 98504-7600 USPS: PO Box 47600, Olympia, WA 98504-7600 Parcels: 300 Desmond Drive, Lacey, WA 98503-1274 On Fri, 17 Aug 2012, penguins wrote:> Can anyone tell me how to remove several numbers for a sequence. For example: > > xx<- c(1,5,7,10) > yy<-seq(1,10,1) > > how do I get take xx away from yy to get the new sequence > > 2,3,4,6,8,9 > > Many thanks in advance > > > > -- > View this message in context: http://r.789695.n4.nabble.com/Remove-several-numbers-from-a-sequence-tp4640630.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >
HI, Try this: ? yy[is.na(match(yy,xx))] #[1] 2 3 4 6 8 9 A.K. ----- Original Message ----- From: penguins <catrsw at bas.ac.uk> To: r-help at r-project.org Cc: Sent: Friday, August 17, 2012 12:18 PM Subject: [R] Remove several numbers from a sequence Can anyone tell me how to remove several numbers for a sequence. For example: xx<- c(1,5,7,10) yy<-seq(1,10,1) how do I get take xx away from yy to get the new sequence 2,3,4,6,8,9 Many thanks in advance -- View this message in context: http://r.789695.n4.nabble.com/Remove-several-numbers-from-a-sequence-tp4640630.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ 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.
On 17-08-2012, at 18:18, penguins wrote:> Can anyone tell me how to remove several numbers for a sequence. For example: > > xx<- c(1,5,7,10) > yy<-seq(1,10,1) > > how do I get take xx away from yy to get the new sequence > > 2,3,4,6,8,9 >You can also do this yy[!(yy %in% xx)] Berend
Hi! Both yy[!(yy %in% xx)] and yy[is.na(match(yy,xx))] work a treat! Developing this topic can anyone tell me how to extract rows which are not replicated across 2 data frames. For example: z1<-c(1,2,3,4) z2<-c(5,6,7,8) zz<- data.frame(cbind(z1,z2)) x1<-c(3,4) x2<-c(7,8) xx<- data.frame(cbind(x1,x2)) so the result would be: 1 5 2 6 Using setdiff(xx,zz) I can get the replicated rows but not the unique ones, and setdiff(zz,xx) returns the all rows. Many thanks -- View this message in context: http://r.789695.n4.nabble.com/Remove-several-numbers-from-a-sequence-tp4640630p4640771.html Sent from the R help mailing list archive at Nabble.com.
Hi> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of penguins > Sent: Monday, August 20, 2012 1:15 PM > To: r-help at r-project.org > Subject: Re: [R] Remove several numbers from a sequence > > Hi! > > Both yy[!(yy %in% xx)] and yy[is.na(match(yy,xx))] work a treat! > > Developing this topic can anyone tell me how to extract rows which are > not replicated across 2 data frames. For example: > > z1<-c(1,2,3,4) > z2<-c(5,6,7,8) > zz<- data.frame(cbind(z1,z2)) > > x1<-c(3,4) > x2<-c(7,8) > xx<- data.frame(cbind(x1,x2)) > > so the result would be: > > 1 5 > 2 6 > > Using setdiff(xx,zz) I can get the replicated rows but not the unique > ones, and setdiff(zz,xx) returns the all rows.I am not sure how setdiff works with data.frames }nothing is said in help page so you need to go to source. sapply((zz), function(x) x %in% t(xx)) gives you data frame with true values but I am not sure if it works only for this example or globally. After that just check number of true values and select appropriate rows. Regards Petr> > Many thanks > > > > > > > > -- > View this message in context: http://r.789695.n4.nabble.com/Remove- > several-numbers-from-a-sequence-tp4640630p4640771.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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.
many thanks for lots of different solutions. I have a couple of very large data frames but using the unique identifier for each row and Arun's solution zz[is.na(match(zz$z1,xx$x1)),] has worked perfectly, Thanks! -- View this message in context: http://r.789695.n4.nabble.com/Remove-several-numbers-from-a-sequence-tp4640630p4640799.html Sent from the R help mailing list archive at Nabble.com.