[R] Romoving elements from a vector. Looking for the opposite of c(), New user

jim holtman jholtman at gmail.com
Thu Nov 15 22:23:05 CET 2007


If what you want to do is to 'remove' from 'x' all the occurrances of
the matching values in 'z' and then create a new sequence, here is a
way of doing it:

> x <- c(3,3,4,4,4,4,5,5,6,7,8,8,9)
> z <- c(1,2,3,4,4,5,5,5)
> #want to remove the matching number in 'z' from 'x'
> # determine which numbers are in each sequence
> x.t <- table(x)
> z.t <- table(z)
> # find which numbers are in common
> inter <- intersect(names(x.t), names(z.t))
> # adjust the counts in 'x.t'
> x.t[inter] <- x.t[inter] - z.t[inter]
> # get rid of any values less than 1
> x.t <- x.t[x.t > 0]
> # now create the new sequence
> rep(as.numeric(names(x.t)), x.t)
[1] 3 4 4 6 7 8 8 9
>


On Nov 15, 2007 2:10 PM, Marc Schwartz <marc_schwartz at comcast.net> wrote:
> Jim,
>
> The one issue with those is that they remove duplicate elements in each
> vector before applying their logic. Thus, would not likely work here:
>
> x <- c(3,3,4,4,4,4,5,5,6,8)
> z <- c(3,4,4,5,5)
>
> In effect, you end up with:
>
> > unique(x)
> [1] 3 4 5 6 8
>
> > unique(z)
> [1] 3 4 5
>
>
> Thus:
>
> > setdiff(x, z)
> [1] 6 8
>
> > setdiff(z, x)
> numeric(0)
>
> > union(x, z)
> [1] 3 4 5 6 8
>
> > intersect(x, z)
> [1] 3 4 5
>
>
> I am also not sure that the two solutions proposed using "%in%" work as
> desired, though I may be missing something there.
>
> I may have also missed other solutions in this thread, but here is a
> proposal:
>
> x <- c(3,3,4,4,4,4,5,5,6,8)
> z <- c(3,4,4,5,5)
>
> for (i in seq(length(z)))
> {
>  Ind <- match(z[i], x)
>  x <- x[-Ind]
> }
>
> > x
> [1] 3 4 4 6 8
>
> I think that works.
>
> HTH,
>
> Marc
>
>
> On Thu, 2007-11-15 at 13:28 -0500, jim holtman wrote:
> > You can also check out the 'set' operations: setdiff, intersect, union.
> >
> > On Nov 15, 2007 12:08 PM, John Kane <jrkrideau at yahoo.ca> wrote:
> > > I think you've read Thomas's request in reverse. and
> > > what he want is:
> > > x[!x %in% z]
> > >
> > > Thanks for the %in% approach BTW.
> > >
> > > --- 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!
> > > >
>
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?


More information about the R-help mailing list