I am trying to find multiple strings in my dataframe, using the which function. I am trying to extend the answer from Find string in data.frame
An example dataframe is:
df1 <- data.frame(animal=c('a','b','c','two', 'five', 'c'), level=c('five','one','three',30,'horse', 'five'), length=c(10, 20, 30, 'horse', 'eight', 'c'))
1 a five 10
2 b one 20
3 c three 30
4 two 30 horse
5 five horse eight
6 c five c
on this dataframe when I apply the which function for one string, I get the correct output e.g.
which(df1 =="c" , arr.ind = T);df1
gives:
row col
[1,] 3 1
[2,] 6 1
[3,] 6 3
But when I try to search for multiple strings, I get only a partially correct output e.g.
which(df1 ==c("c", "horse", "five") , arr.ind = T)
row col
[1,] 5 2
[2,] 6 2
The expected output should be:
row col
[1,] 3 1
[2,] 5 1
[3,] 6 1
[4,] 1 2
[5,] 5 2
[6,] 6 2
[7,] 4 3
[8,] 6 3
Hence my question:
why does the solution with c("c", "horse", "five") not work?
I have tried with
which(df1=="c" | df1=="horse" | df1 =="five", arr.ind = T)
that gives me the correct output, but for many strings is too lengthy, how can I make my code succinct?