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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:21:41+00:00 2026-06-18T02:21:41+00:00

Having df1 and df2 as follows: df1 <- read.table(text = x y z 1

  • 0

Having df1 and df2 as follows:

df1 <- read.table(text =" x y z
                          1 1 1
                          1 2 1
                          1 1 2
                          2 1 1
                          2 2 2",header=TRUE)

df2 <- read.table(text =" a b c
                          1 1 1
                          1 2 8
                          1 1 2
                          2 6 2",header=TRUE)

I can ask of the data a bunch of things like:

 df2[ df2$b == 6 | df2$c == 8 ,] #any rows where b=6 plus c=8 in df2
 #and additive conditions
 df2[ df2$b == 6 & df2$c == 8 ,] # zero rows

between data.frame:

 df1[ df1$z %in% df2$c ,] # rows in df1 where values in z are in c (allrows)

This gives me all rows:

 df1[ (df1$x %in%  df2$a) &
      (df1$y %in%  df2$b) &
      (df1$z %in%  df2$c) ,]

but shouldn’t this give me all rows of df1 too:

 df1[ df1$z %in% df2$c | df1$b == 9,]

What I am really hoping to do is to subset df1 an df2 on three column conditions,
so that I only get rows in df1 where a,b,c all equal x,y,z at the same time within a row. In real data i will have more than 3 columns but I will still want to subset on 3 additive column conditions.

So subsetting my example data df1 on df2 my result would be:

df1
   1 1 1
   1 1 2

Playing with syntax has confusedme more and the SO posts are all variaion of what I want that actually lead to more confusion for me.

I figured out I can do this:

 merge(df1,df2, by.x=c("x","y","z"),by.y=c("a","b","c"))

which gives me what I want, but I would like to understand why I am wrong in my [ attempts.

  • 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-18T02:21:42+00:00Added an answer on June 18, 2026 at 2:21 am

    In addition to your nice solution using merge (thanks for that, I always forget merge), this can be achieved in base using ?interaction as follows. There may be other variations of this, but this is the one I am familiar with:

    > df1[interaction(df1) %in% interaction(df2), ]
    

    Now to answer your question: First, I think there’s a typo (corrected) in:

    df1[ df1$z %in% df2$c | df2$b == 9,] # second part should be df2$b == 9
    

    You would get an error, because the first part evaluates to

    [1] TRUE TRUE TRUE TRUE TRUE
    

    and the second evaluates to:

    [1] FALSE FALSE FALSE FALSE
    

    You do a | operation on unequal lengths getting the error:

    longer object length is not a multiple of shorter object length
    

    Edit: If you have multiple columns then you can choose the interaction as such. For example, if you want to get from df1 the rows where the first two columns match with that of df2, then you could simply do:

    > df1[interaction(df1[, 1:2]) %in% interaction(df2[, 1:2]), ]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Having read this past question for git, I would like to ask if there
Having read the documentation for VPython and GTK threading , it seems to me
Having trouble understanding how to filter an images table by tag information in a
I'm trying to write a data frame to a gzip file but having problems.
Having used to debug with tools like gdb etc, I have little knowledge about
Having class code autogenerated by WCF/svcutil.exe like that: public class Foo { private float
Having a headache with IE. I have an image (24x24) which I'd like to
Having properly configured a Development server and a Production server, I would like to
I have a very big data frame consisting of data like this: PENR ANFDAT
Having just read the recent article in Wired , I'm curious: what is it

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.