How to replace levels with certain value? I have a categorical variable in my dataset. I want to replace the levels which are present in test dataset and not present in training set with a value called "others"

Here is how it looks:

levels(training$var1) has levels as "1"  "2"  "3"  "Others"       

levels(testing$var1) has levels as "1"  "2"  "3"  "4"  "5"  "6"  "7"  "8"  "9"  "10"  

I want to replace all the levels in testing data which are not training data. To achieve this I take the difference between the levels first.   

a &lt;- setdiff(levels(levels(testing$var1),training$var1)).  


and I get output as  

levels(a) as "4"  "5"  "6"  "7"  "8"  "9"  "10"  

Now I need to replace all the above difference values with "Others". Kindly note that I do not want to drop out these levels but I want to replace with "Others".  

For this I tried  

testing$var1[testing$var1 == "4" &lt;- 'Others"  
testing$var1[testing$var1 == "5" &lt;- 'Others" ==&gt; these works.  


However I want to make it in run time, something like this:  

testing$var1[testing$var1 == a[1,] &lt;- 'Others"


But this is not working.
I could be able to achieve this with the following code:

testing$var1 &lt;- as.character(testing$var1)
a &lt;- data.frame(a)
testing$var1 [testing$var1 %in% a[1,] &lt;- "Others"
testing$var1 &lt;- as.factor(testing$var1)


In case if there is any other better/effective solution/function to achieve this, please let me know. 

Thanks all.