You can achieve this in R by using the %in% operator to filter rows in one dataframe based on values in a column of another dataframe. Here's how you can do it:
main.r366 chars13 lines
In this example, df1$col1 %in% df2$col2 checks for each value in df1$col1 if it exists in df2$col2, returning a logical vector that is then used to subset df1. This will return all rows from df1 where col1 matches any value in col2 of df2.
You can also achieve the same result using dplyr package's filter function in combination with map_lgl from the purrr package or using inner_join for more complex scenarios.
For instance, using dplyr and inner_join:
main.r180 chars9 lines
This will give you the rows where there is a match between col1 in df1 and col2 in df2, effectively filtering df1 to only include rows that match any value in df2$col2.
gistlibby LogSnag