Thomas Frööjd
2007-Nov-15 14:15 UTC
[R] Romoving elements from a vector. Looking for the opposite of c(), New user
Hi I have three vectors say x, y, z. One of them, x contains observations on a variable. To x I want to append all observations from y and remove all from z. For appending c() is easily used x <- c(x,y) But how do I remove all observations in z from x? You can say I am looking for the opposite of c(). Best regards
Charilaos Skiadas
2007-Nov-15 14:29 UTC
[R] Romoving elements from a vector. Looking for the opposite of c(), New user
On Nov 15, 2007, at 9:15 AM, Thomas Fr??jd wrote:> Hi > > I have three vectors say x, y, z. One of them, x contains observations > on a variable. To x I want to append all observations from y and > remove all from z. For appending c() is easily used > > x <- c(x,y) > > But how do I remove all observations in z from x? You can say I am > looking for the opposite of c().If you are looking for the opposite of c, provided you want to remove the first part of things, then perhaps this would work: z<-c(x,y) z[-(1:length(x))] However, if you wanted to remove all appearances of elements of x from c(x,y), regardless of whether those elements appear in the x part of in the y part, I think you would want: z[!z %in% x] Probably there are other ways. Welcome to R!> Best regardsHaris Skiadas Department of Mathematics and Computer Science Hanover College
Thomas Frööjd
2007-Nov-15 16:09 UTC
[R] Romoving elements from a vector. Looking for the opposite of c(), New user
Not sure i explained it good enough. Ill try with an example say x=[3,3,4,4,4,4,5,5,6,8] z=[3,4,4,5,5] what i want to get after removing z from x is something like x=[3,4,4,6,8] On Nov 15, 2007 3:29 PM, Charilaos Skiadas <cskiadas at gmail.com> wrote:> > > On Nov 15, 2007, at 9:15 AM, Thomas Fr??jd wrote: > > > Hi > > > > I have three vectors say x, y, z. One of them, x contains observations > > on a variable. To x I want to append all observations from y and > > remove all from z. For appending c() is easily used > > > > x <- c(x,y) > > > > But how do I remove all observations in z from x? You can say I am > > looking for the opposite of c(). > > If you are looking for the opposite of c, provided you want to remove > the first part of things, then perhaps this would work: > > z<-c(x,y) > z[-(1:length(x))] > > However, if you wanted to remove all appearances of elements of x > from c(x,y), regardless of whether those elements appear in the x > part of in the y part, I think you would want: > > z[!z %in% x] > > Probably there are other ways. > > Welcome to R! > > > Best regards > > Haris Skiadas > Department of Mathematics and Computer Science > Hanover College > > ______________________________________________ > 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. >
Thibaut Jombart
2007-Nov-15 16:31 UTC
[R] Romoving elements from a vector. Looking for the opposite of c(), New user
Thomas Fr??jd wrote:>Not sure i explained it good enough. Ill try with an example > >say > >x=[3,3,4,4,4,4,5,5,6,8] >z=[3,4,4,5,5] > >what i want to get after removing z from x is something like >x=[3,4,4,6,8] > > >On Nov 15, 2007 3:29 PM, Charilaos Skiadas <cskiadas at gmail.com> wrote: > > >>On Nov 15, 2007, at 9:15 AM, Thomas Fr??jd wrote: >> >> >> >>>Hi >>> >>>I have three vectors say x, y, z. One of them, x contains observations >>>on a variable. To x I want to append all observations from y and >>>remove all from z. For appending c() is easily used >>> >>>x <- c(x,y) >>> >>>But how do I remove all observations in z from x? You can say I am >>>looking for the opposite of c(). >>> >>> >>If you are looking for the opposite of c, provided you want to remove >>the first part of things, then perhaps this would work: >> >>z<-c(x,y) >>z[-(1:length(x))] >> >>However, if you wanted to remove all appearances of elements of x >>from c(x,y), regardless of whether those elements appear in the x >>part of in the y part, I think you would want: >> >>z[!z %in% x] >> >>Probably there are other ways. >> >>Welcome to R! >> >> >> >>>Best regards >>> >>> >>Haris Skiadas >>Department of Mathematics and Computer Science >>Hanover College >> >>______________________________________________ >>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. >> >> >> > >______________________________________________ >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, you may try this : x=[3,3,4,4,4,4,5,5,6,8] z=c(3,4,4,5,5) f1 <- function(vec,toremove){ + + for(elem in toremove){ + temp <- grep(elem,vec)[1] + if(!is.na(temp)) vec <- vec[-temp] + } + + return(vec) + } > f1(x,z) [1] 3 4 4 6 8 Regards, Thibaut. -- ###################################### Thibaut JOMBART CNRS UMR 5558 - Laboratoire de Biom?trie et Biologie Evolutive Universite Lyon 1 43 bd du 11 novembre 1918 69622 Villeurbanne Cedex T?l. : 04.72.43.29.35 Fax : 04.72.43.13.88 jombart at biomserv.univ-lyon1.fr http://lbbe.univ-lyon1.fr/-Jombart-Thibaut-.html?lang=en http://pbil.univ-lyon1.fr/software/adegenet/
Gabor Grothendieck
2007-Nov-15 21:54 UTC
[R] Romoving elements from a vector. Looking for the opposite of c(), New user
Try this. xx and zz are the same as x and z except they have a sequence number appended. We then do a setdiff and remove the sequence numbers.> xx <- paste(x, seq(x) - match(x, x)) > zz <- paste(z, seq(z) - match(z, z)) > dd <- setdiff(xx, zz) > as.numeric(sub(" .*", "", dd))[1] 3 4 4 6 7 8 8 9 On Nov 15, 2007 11:09 AM, Thomas Fr??jd <tfrojd at gmail.com> wrote:> Not sure i explained it good enough. Ill try with an example > > say > > x=[3,3,4,4,4,4,5,5,6,8] > z=[3,4,4,5,5] > > what i want to get after removing z from x is something like > x=[3,4,4,6,8] > > > On Nov 15, 2007 3:29 PM, Charilaos Skiadas <cskiadas at gmail.com> wrote: > > > > > > On Nov 15, 2007, at 9:15 AM, Thomas Fr??jd wrote: > > > > > Hi > > > > > > I have three vectors say x, y, z. One of them, x contains observations > > > on a variable. To x I want to append all observations from y and > > > remove all from z. For appending c() is easily used > > > > > > x <- c(x,y) > > > > > > But how do I remove all observations in z from x? You can say I am > > > looking for the opposite of c(). > > > > If you are looking for the opposite of c, provided you want to remove > > the first part of things, then perhaps this would work: > > > > z<-c(x,y) > > z[-(1:length(x))] > > > > However, if you wanted to remove all appearances of elements of x > > from c(x,y), regardless of whether those elements appear in the x > > part of in the y part, I think you would want: > > > > z[!z %in% x] > > > > Probably there are other ways. > > > > Welcome to R! > > > > > Best regards > > > > Haris Skiadas > > Department of Mathematics and Computer Science > > Hanover College > > > > ______________________________________________ > > 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. > > > > ______________________________________________ > 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. >
Gabor Grothendieck
2007-Nov-15 22:04 UTC
[R] Romoving elements from a vector. Looking for the opposite of c(), New user
And here is a slightly shorter variation that encodes the sequence number as the last two digits and then removes it at the end: xx <- 100 * x + seq(x) - match(x, x) zz <- 100 * z + seq(z) - match(z, z) setdiff(xx, zz) %/% 100 On Nov 15, 2007 4:54 PM, Gabor Grothendieck <ggrothendieck at gmail.com> wrote:> Try this. xx and zz are the same as x and z except > they have a sequence number appended. We then do > a setdiff and remove the sequence numbers. > > > xx <- paste(x, seq(x) - match(x, x)) > > zz <- paste(z, seq(z) - match(z, z)) > > dd <- setdiff(xx, zz) > > as.numeric(sub(" .*", "", dd)) > [1] 3 4 4 6 7 8 8 9 > > > > On Nov 15, 2007 11:09 AM, Thomas Fr??jd <tfrojd at gmail.com> wrote: > > Not sure i explained it good enough. Ill try with an example > > > > say > > > > x=[3,3,4,4,4,4,5,5,6,8] > > z=[3,4,4,5,5] > > > > what i want to get after removing z from x is something like > > x=[3,4,4,6,8] > > > > > > On Nov 15, 2007 3:29 PM, Charilaos Skiadas <cskiadas at gmail.com> wrote: > > > > > > > > > On Nov 15, 2007, at 9:15 AM, Thomas Fr??jd wrote: > > > > > > > Hi > > > > > > > > I have three vectors say x, y, z. One of them, x contains observations > > > > on a variable. To x I want to append all observations from y and > > > > remove all from z. For appending c() is easily used > > > > > > > > x <- c(x,y) > > > > > > > > But how do I remove all observations in z from x? You can say I am > > > > looking for the opposite of c(). > > > > > > If you are looking for the opposite of c, provided you want to remove > > > the first part of things, then perhaps this would work: > > > > > > z<-c(x,y) > > > z[-(1:length(x))] > > > > > > However, if you wanted to remove all appearances of elements of x > > > from c(x,y), regardless of whether those elements appear in the x > > > part of in the y part, I think you would want: > > > > > > z[!z %in% x] > > > > > > Probably there are other ways. > > > > > > Welcome to R! > > > > > > > Best regards > > > > > > Haris Skiadas > > > Department of Mathematics and Computer Science > > > Hanover College > > > > > > ______________________________________________ > > > 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. > > > > > > > ______________________________________________ > > 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. > > >