filter out rows where two columns don't match in r

You can filter out rows where two columns don't match in R using the following approaches:

Using Base R

main.r
# Create a sample dataframe
df <- data.frame(name = c("John", "Mary", "David", "John"),
                 nickname = c("John", "Jane", "David", "Jim"))

# Filter out rows where 'name' and 'nickname' don't match
filtered_df <- df[df$name == df$nickname, ]

# Print the filtered dataframe
print(filtered_df)
305 chars
10 lines

Using dplyr

main.r
# Load the dplyr library
library(dplyr)

# Create a sample dataframe
df <- data.frame(name = c("John", "Mary", "David", "John"),
                 nickname = c("John", "Jane", "David", "Jim"))

# Filter out rows where 'name' and 'nickname' don't match
filtered_df <- df %>% 
  filter(name == nickname)

# Print the filtered dataframe
print(filtered_df)
352 chars
14 lines

Both of these approaches will return a dataframe with only the rows where the name and nickname columns match.

related categories

gistlibby LogSnag