Mike
2004-May-05 03:14 UTC
[R] anyone know how to combine two vector with some # overlaped?
Hi, there, Suppose I have two vector say x=c(1 2 3 4 5) and y=(2 3 6 7). Then I want to combine these two vector together and get z=c(1 2 3 4 5 6 7) with 2 and 3 only appear once. I want to extend this one to a general case(say more than 100 elements in x and y and each time I don't know which elements are the same). Do you happen to know how to do this and which command should use? Thank you very much. Please reply to this email. Any kind help would be greatly appreciated. Mike
Julian Taylor
2004-May-05 03:26 UTC
[R] anyone know how to combine two vector with some # overlaped?
Try x <- c(1,2,3,4,5) y <- c(2,3,6,7) z <- c(x,y)[!duplicated(c(x,y))] HTH, Jules Mike wrote:> > Hi, there, > > Suppose I have two vector say x=c(1 2 3 4 5) and y=(2 > 3 6 7). Then I want to combine these two vector > together and get z=c(1 2 3 4 5 6 7) with 2 and 3 only > appear once. I want to extend this one to a general > case(say more than 100 elements in x and y and each > time I don't know which elements are the same). Do you > happen to know how to do this and which command should > use? > > Thank you very much. Please reply to this email. Any > kind help would be greatly appreciated. > > Mike > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html-- --- Julian Taylor phone: +61 8 8303 6751 ARC Research Associate fax: +61 8 8303 6760 BiometricsSA, mobile: +61 4 1638 8180 University of Adelaide/SARDI email: julian.taylor at adelaide.edu.au Private Mail Bag 1 www: http://www.BiometricsSA.adelaide.edu.au Glen Osmond SA 5064 "There is no spoon." -- Orphan boy ---
Uwe Ligges
2004-May-05 06:26 UTC
[R] anyone know how to combine two vector with some # overlaped?
Mike wrote:> Hi, there, > > Suppose I have two vector say x=c(1 2 3 4 5) and y=(2 > 3 6 7). Then I want to combine these two vector > together and get z=c(1 2 3 4 5 6 7) with 2 and 3 only > appear once. I want to extend this one to a general > case(say more than 100 elements in x and y and each > time I don't know which elements are the same). Do you > happen to know how to do this and which command should > use? > > Thank you very much. Please reply to this email. Any > kind help would be greatly appreciated. > > Mike > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.htmlSee ?unique Uwe Ligges
pallier
2004-May-05 07:29 UTC
[R] anyone know how to combine two vector with some # overlaped?
Mike wrote:>Hi, there, > >Suppose I have two vector say x=c(1 2 3 4 5) and y=(2 >3 6 7). Then I want to combine these two vector >together and get z=c(1 2 3 4 5 6 7) with 2 and 3 only >appear once. >union(x,y) R provides several set operators. See ?union. Christophe Pallier
Richard A. O'Keefe
2004-May-05 08:08 UTC
[R] anyone know how to combine two vector with some # overlaped?
If you want this: > Suppose I have two vector say x=c(1 2 3 4 5) and y=(2 > 3 6 7). Then I want to combine these two vector > together and get z=c(1 2 3 4 5 6 7) with 2 and 3 only > appear once. Julian Taylor <julian.taylor at adelaide.edu.au> suggests: x <- c(1,2,3,4,5) y <- c(2,3,6,7) z <- c(x,y)[!duplicated(c(x,y))] But you can do it in one step: z <- unique(c(x,y)) I don't know how unique() is implemented, but using a hash table it _could_ be done in linear expected time, and in practice it seems to be pretty quick, more than quick enough for a few hundred elements.