0

I have a dataframe which looks like the following:

library(tidyverse)

full.df<-tibble(x = c(1.5, 2.5, 3.5, 5.6, 7.4),
                y = c(4.5, 4.3, 2.6, 5.6, 7.6),
                a = c(1, 2, 3, 4, 5),
                b = c(5, 6, 7, 8, 9))

And a second dataframe that's essentially a subset of full.df but missing columns a & b:

subset.df<-tibble(x = c(1.5, 3.5, 7.4), 
                  y = c(4.5, 2.6, 7.6))

How can I filter full.df on x & y using x & y from subset.df? I wish to return this:

# A tibble: 3 x 4
      x     y     a     b
  <dbl> <dbl> <dbl> <dbl>
1   1.5   4.5     1     5
2   3.5   2.6     3     7
3   7.4   7.6     5     9

I have tried full.df %>% filter(x == subset.df$x & y == subset.df$y) but this only returns the first row.

Simon
  • 991
  • 8
  • 30

1 Answers1

2

You're looking for

full.df %>% right_join(subset.df, by = c('x','y'))
geotheory
  • 22,624
  • 29
  • 119
  • 196
  • Is this homework?! – geotheory Apr 24 '20 at 10:00
  • Wow, my brain really isn't working today. Can't believe I didn't think of this. – Simon Apr 24 '20 at 10:00
  • Ha, no. It's for a paper I'm working on, if you can believe it. I just got stuck down the `filter` route and didn't even consider `join`. – Simon Apr 24 '20 at 10:01
  • 1
    Fair enough! The less elegant filter approach would be `filter(full.df, paste(x,y) %in% paste(subset.df$x, subset.df$y))` – geotheory Apr 24 '20 at 10:03
  • I've been sat here for like 20 mins thinking filter, filter, filter. I use joins frequently so I'm baffled as to why I didn't think of it. I've been working too much recently, I think my brain is turning to mush. – Simon Apr 24 '20 at 10:04
  • 1
    Stretch your legs and have a cuppa! – geotheory Apr 24 '20 at 10:07