How to select observations in df 1 based on df 2?

Hello, I have 2 data frames. df 1 is called survey. df 2 is called admin. Both data frames contain first name and last name column. I want to select observations in df 1 that they also belong to df 2. (Because I want to know these members explicitly). What is the code?

Hi @Tung,

you could do this e.g. with an inner_join on all relevant columns.

library(tidyverse)
tbl1 <- tibble(FIRST = c("John", "Joe", "Mary"),
               LAST = c("A", "B", "C"),
               MORE = c(34,87,99))
tbl2 <- tibble(FIRST = c("Mary"),
               LAST = c("C"),
               EVEN_MORE = c(111))

# both
inner_join(tbl1, tbl2, by = c("FIRST", "LAST"))
#> # A tibble: 1 x 4
#>   FIRST LAST   MORE EVEN_MORE
#>   <chr> <chr> <dbl>     <dbl>
#> 1 Mary  C        99       111

Created on 2020-10-23 by the reprex package (v0.3.0)

Hi @smichal,

I tried your solution. But

Error: Join columns must be present in data.
x Problem with First name and Last name.
Run rlang::last_error() to see where the error occurred.

It's weird. I quit R and reopen R, and your code works. It sees such things always happen in my computer. Why?

Hi @Tung,

sorry, from this point on I could only guess.

Could you make a reproducible example (reprex) to make things clearer?

(My guess would be that you have not given the correct column names to join on. )

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.