Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8090163
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T19:37:52+00:00 2026-06-05T19:37:52+00:00

Let say I have the following data in R. training = factor(c(1,1,3,2,1,3,2,34,67,34)) test =

  • 0

Let say I have the following data in R.

training = factor(c(1,1,3,2,1,3,2,34,67,34))
test = factor(c(1,1,2,30,65,30))

(my data is much more complicated, this is a simplification)

I want to check if the levels in the test set exist in the training set, and if not to replace it by the nearest value in the training set.
For example, the levels 30 and 65 in test set do not exist in training set, so I want to replace them by 34 and 67 respectively.

Currently, I created the following code.

replacefactor <- function(dat,new_factor,near_factor) {
if (!(near_factor %in% levels(dat))){
    levels(dat) <- c(levels(dat),near_factor)
}
dat[dat==new_factor] <- near_factor
dat <- factor(dat)
}

test <- replacefactor(test,30,34)
test <- replacefactor(test,65,67)

It works, but I need to specify the levels by hand. This is not practical for me due to the size of my data.

I am not sure how I could find the nearest value in the training set.
I could then use a for loop to automate it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T19:37:54+00:00Added an answer on June 5, 2026 at 7:37 pm

    first get the levels that aren’t matched:

    test.missing <- levels(test)[!levels(test) %in% levels(training)]
    

    then write a function to run along them and find the nearest match:

    myfun <- function(x, y) {
      levels(y)[which.min(abs(as.integer(levels(y)) - as.integer(x)))]
    }
    
    > unlist(lapply(test.missing, myfun, training))
    [1] "34" "67"
    

    Then this can be assigned to the correct levels:

    levels(test)[!levels(test) %in% levels(training)] <- unlist(lapply(test.missing, myfun, training))
    
    > levels(test)
    [1] "1"  "2"  "34" "67"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have the following data in the Customers table: (nothing more) ID
Let's say I have the following string: something1: +12.0 (some unnecessary trailing data (this
Let's say I have a table containing following data: | id | t0 |
Let's say i have a table with the following data: day1 item1 30 day1
Let's say I have the following code: <ul> <li data-val=1>1</li> <li data-val=4>2</li> <li data-val=2>3</li>
Let's say I have following table like this <!DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0
Let's say we have following this: <p class=first>This is paragraph 1.</p> <p class=second>This is
Let's say we have the following Haskell: data T = T0 | T1 |
Let's say I have the following rows: id data (TEXT) 1 abc100dfg 2 abc200dfg
Let's say I have a database filled with people with the following data elements:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.