I have a data frame with a variable containing elements to drop if they match to an element in another variable - see a small example below:
df <- data.frame(pair = c(1, 1, 2, 2, 3, 3), animal = rep(c("dog", "cat"), 3), value = seq(1, 12, 2), drop = c("no", "no", "dog", "dog", "cat", "cat")) pair animal value drop 1 1 dog 1 no 2 1 cat 3 no 3 2 dog 5 dog 4 2 cat 7 dog 5 3 dog 9 cat 6 3 cat 11 cat I'm trying to want to filter the data frame according to whether the value of animal matches the value of drop. I want something like filter(df, animal != drop) to remove rows where only the value of animal matches the value of drop:
pair animal value drop 1 1 dog 1 no 2 1 cat 3 no 4 2 cat 7 dog 5 3 dog 9 cat I also tried writing a simple loop to test whether animal matches drop for each row and remove the row if true, but I couldn't get it working. (I'm not very confident with loops and would prefer not to use one if possible as my data frame is very large but I was getting desperate!)
for(i in nrow(df)){ if(df$animal[i] == df$drop[i]){ df <- df[-i,] return(df) } } Is there a way of doing this using dplyr?