filter rows of a dataset that match any values in any row or column in another dataframe in r in r

You can use the %in% operator in R to filter rows of a dataset that match any values in any row or column in another dataframe. Here's an example:

main.r
# Load necessary libraries
library(dplyr)

# Create the first dataframe
df1 <- data.frame(
  col1 = c(1, 2, 3, 4, 5),
  col2 = c("a", "b", "c", "d", "e"),
  col3 = c(10, 20, 30, 40, 50)
)

# Create the second dataframe
df2 <- data.frame(
  colA = c(2, 4, "b", "d", 30),
  colB = c(3, 5, "c", "e", 40)
)

# Get all unique values from df2
unique_values_df2 <- unique(c(as.vector(df2)))

# Filter rows of df1 that match any values in unique_values_df2
filtered_df1 <- df1 %>% 
  filter(if_any(everything(), ~ .x %in% unique_values_df2))

# Print the filtered dataframe
print(filtered_df1)
586 chars
26 lines

In this example, df1 is the original dataframe and df2 is the dataframe containing the values to match. The unique function is used to get all unique values from df2. Then, the filter function from the dplyr package is used to filter rows of df1 that match any values in unique_values_df2. The if_any function is used to apply the condition to all columns of df1.

related categories

gistlibby LogSnag