user9974638
user9974638

Reputation: 199

Filter data where col A != col B

library(magrittr)
library(dplyr)
A <- c('X', 'X', 'Y', 'Y')
B <- c('X', 'X', 'Y', 'Z')

sample_df <- data.frame(A, B)

sample_df %>% filter(A!=B)

I would like to select all rows where the values of A and B don't agree (and A and B are factors); the code above throws the error "Error in Ops.factor(A, B) : level sets of factors are different"

Upvotes: 0

Views: 54

Answers (1)

Matt
Matt

Reputation: 7385

Expanding on Gregor's comment:

Using dplyr:

sample_df %>% 
  mutate_all(., as.character) %>%
  filter(A!=B)

Upvotes: 1

Related Questions