I've been trying to do this with mutate(), str_detect(), but it's too complex and dont know how to do all the steps, please help!
I have a dataframe in which 3 cols contain let's say fruits, animals, or "none".
col1 | col2 | col3 |
---|---|---|
apple | cat | none |
apple | dog | none |
pear | none | none |
pear | apple | none |
none | none | none |
And then I have two lists:
fruit <- c("apple", "pear", banana") animal <- c("cat", "dog", "sheep")
I want to create two new columns in the dataframe: col4 should display only fruits from col1, col2, col3. If more than one fruit, I need them separated by commas. col5 does the same but for animals. If col1, col2, col3 dont contain an animal or a fruit, I need col4 and col5 to say "none".
col1 | col2 | col3 | col4 | col5 |
---|---|---|---|---|
apple | cat | none | apple | cat |
cat | dog | none | none | cat, dog |
pear | none | none | pear | none |
pear | apple | none | pear, apple | none |
none | none | none | none | none |