1
$\begingroup$

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 <- 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" <- 'Others"  
testing$var1[testing$var1 == "5" <- 'Others" ==> these works.  

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

testing$var1[testing$var1 == a[1,] <- 'Others"

But this is not working.

$\endgroup$
3
$\begingroup$

I could be able to achieve this with the following code:

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

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

Thanks all.

| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.