I'm trying to create a filter to remove lines from a dataset using grep and subset together.
Sample dataset:
id <- 1:10
problem <- c("a" , "b", "c", "d", "a","b","c","a", "b", "a")
solution1 <- c("eat", "sleep", "drink", "play", "sleep", "play", "play", "drink", "play", "eat")
solution2 <- c("read", "read", "eat", "drink", "eat", "sleep", "eat", "read", "eat", "play")
df <- c(id, problem, solution1, solution2)
I'm trying to remove those rows with problem "a" and have "eat" in either solution1 or solution2.
The result is that it should remove id 1, 5 and 10.
I have tried using:
df <- subset(df, problem=="a" & !(grepl("eat", df)))
and
df <- df[!grepl("eat", df) & grepl("a", df$problem)]
Can't seem to find a similar solution on StackOverflow or on other websites I Googled.
Would appreciate if anyone can help. Thanks!