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 6614455
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:20:28+00:00 2026-05-25T20:20:28+00:00

I want to subset a dataframe which has an ID column ( v1 ,

  • 0

I want to subset a dataframe which has an ID column (v1, all unique) and a “linked” ID column (v2). The expectation of v2 is that it may contain NAs, but where it does, the corresponsing element of v1 does not appear elsewhere in v2. Also, it is expected that the relation between the columns is symmetric: where there is an entry, x, in v2 the v1 entry of that row, y, is mirrored in another row where v1 has x and v2 has y. The last criteria is that the relation is not reflexive: ie x!=y.

I want to subset the dataframe to the elements which don’t fit the expected criteria.

Here is some sample data to illustrate:

set.seed(1)
dfr <- data.frame(v1=letters,v2=rev(letters))
dfr[sample(26,10),2]<-NA
dfr[sample(26,5),2]<-sample(letters,5)


dfr
   v1   v2
1   a    z
2   b <NA>
3   c    x
4   d    w
5   e <NA>
6   f    u
7   g <NA>
8   h    s
9   i    i
10  j <NA>
11  k    p
12  l <NA>
13  m    f
14  n <NA>
15  o    l
16  p    k
17  q    j
18  r    e
19  s <NA>
20  t    g
21  u <NA>
22  v    e
23  w <NA>
24  x    q
25  y    x
26  z    a

So rows 1, 2, 11, 14, 16, and 26 all meet the criteria, and I want to identify the rest.

I have attempted some solutions using match, but the NAs are causing problems. It also probably relies on the fact that in this case v2 is based on rev(v1), whereas a more general solution can’t make that assumption.

  • 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-05-25T20:20:29+00:00Added an answer on May 25, 2026 at 8:20 pm

    If I correctly understand, here is an example:

    > subset(dfr, (is.na(v2) & !(v1%in%dfr$v2)) | !is.na(v2) & paste(v1, v2) %in% paste(dfr$v2, dfr$v1))
       v1   v2
    1   a    z
    2   b <NA>
    9   i    i
    11  k    p
    14  n <NA>
    16  p    k
    26  z    a
    
    # or if v1 == v2 is not included:
    > subset(dfr, (is.na(v2) & !(v1%in%dfr$v2)) | !is.na(v2) & (v1 != v2 & paste(v1, v2) %in% paste(dfr$v2, dfr$v1)))
       v1   v2
    1   a    z
    2   b <NA>
    11  k    p
    14  n <NA>
    16  p    k
    26  z    a
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My problem is that I have a table, but I only want a subset
tl;dr-edition: I have a compilation I know that will fail, but want the subset
want to have a Hyperlink-Button in a gridView in which I can display a
Want the function to sort the table by HP but if duplicate HPs then
want to ask user to input something but not want to wait forever. There
EDIT : It seems BCP does what I want: Building a subset of boost
I want to get a subset of a string in Javascript. Currently, I only
Every time I want to take a subset of a patch, I'm forced to
I've got a set of static files I want to serve. They're all available
I want to get a subset of a table, here's the example: 1 A

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.