Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8801151
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:45:42+00:00 2026-06-14T00:45:42+00:00

Let’s say I have two vectors: x <- c(1,16,20,7,2) y <- c(1, 7, 5,2,4,16,20,10)

  • 0

Let’s say I have two vectors:

x <- c(1,16,20,7,2)

y <- c(1, 7, 5,2,4,16,20,10)

I want to remove elements in y that are not in x. That is, I want to remove elements 5, 4, 10 from y.

y
[1] 1 7 2 16 20 

In the end, I want vectors x and y to have to same elements. Order does not matter.

My thoughts: The match function lists the indices of the where the two vectors contains a matching element but I need a function is that essentially the opposite. I need a function that displays the indices where the elements in the two vectors don’t match.

# this lists the indices in y that match the elements in x
match(x,y)
[1] 1 6 7 2 4   # these are the indices that I want; I want to remove
                # the other indices from y

Does anyone know how to do this? thank you

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T00:45:44+00:00Added an answer on June 14, 2026 at 12:45 am

    You are after intersect

    intersect(x,y)
    ## [1]  1 16 20  7  2
    

    If you want the indices for the elements of y in x, using which and %in% (%in% uses match internally, so you were on the right track here)

    which(y %in% x)
    ## [1] 1 2 4 6 7
    

    As @joran points out in the comments intersect will drop the duplicates, so perhaps a safe option, if you want to return true matches would be something like

    intersection <- function(x,y){.which <- intersect(x,y)
     .in <- x[which(x %in% y)]
     .in}
    
    
    x <- c(1,1,2,3,4)
    y <- c(1,2,3,3)
    
    intersection(x,y)
    ## [1] 1 1 2 3
    # compare with
    intersect(x,y)
    ## [1] 1 2 3
    
    intersection(y,x)
    ## [1] 1 2 3 3
    # compare with 
    intersect(y, x)
    ## [1] 1 2 3
    

    You then need to be careful about ordering with this modified function (which is avoided with intersect as it drops duplicated elements )


    If you want the index of those element of y not in x, simply prefix with ! as `%in% returns a logical vector

    which(!y%in%x)
    
    ##[1] 3 5 8
    

    Or if you want the elements use setdiff

    setdiff(y,x)
    ## [1]  5  4 10
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have two text files that I need to extract data out
Let say I want to create an iOS app that download music files from
Let's say I have a javascript array with a bunch of elements (anywhere from
Let's say I don't have photoshop, but I want to make pattern files (.pat)
Let me explain best with an example. Say you have node class that can
Let's say that I have a SQLite database that I create in a separate
Let's say I have thousands of users and I want to make the passwords
Let's say we have two pages in Orchard CMS. The homepage and the About
Let's say I have multiple requirements for a password. The first is that the
Let's say that I have a date in R and it's formatted as follows.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.