I am attempting do make a vectorized "find and replace" of multiple strings in a data frame. In my mock data frame below, I want to replace "human" with "dog", and "cat" with "moose" .
Mock input:
df<-data.frame(organism=c("human","cat","bird","virus","bat","pangolian"),size=c(6,4,2,1,3,5))
df
organism size
1 human 6
2 cat 4
3 bird 2
4 virus 1
5 bat 3
6 pangolian 5
expected output:
df1
organism size
1 dog 6
2 moose 4
3 bird 2
4 virus 1
5 bat 3
6 pangolian 5
In reality, I have a large data frame and many replacements, so I would want to have my replacement strings in a vector like this:
replacement<-c("dog","moose")
I know this is supposed to be simply to solve, but as a newbie I simply can't get my head around it. So, thanks in advance.