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 8419825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T02:43:17+00:00 2026-06-10T02:43:17+00:00

Here is an example of a problem I am attempting to solve and implements

  • 0

Here is an example of a problem I am attempting to solve and implements in a much larger database:

I have a sparse grid of points across the new world, with lat and long defined as below.

LAT<-rep(-5:5*10, 5)
LON<-rep(seq(-140, -60, by=20), each=11)

I know the color of some points on my grid

COLOR<-(c(NA,NA,NA,"black",NA,NA,NA,NA,NA,"red",NA,NA,"green",NA,"blue","blue",NA,"blue",NA,NA,"yellow",NA,NA,"yellow",NA+
  NA,NA,NA,"blue",NA,NA,NA,NA,NA,NA,NA,"black",NA,"blue","blue",NA,"blue",NA,NA,"yellow",NA,NA,NA,NA,"red",NA,NA,"green",NA,"blue","blue"))
data<-as.data.frame(cbind(LAT,LON,COLOR))

What I want to do is replace the NA values in COLOR with the color that is closeset (in distance) to that point. In the actual implementation, I am not worried too much with ties, but I suppose it is possible (I could probably fix those by hand).

Thanks

  • 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-10T02:43:19+00:00Added an answer on June 10, 2026 at 2:43 am

    Yup.

    First, make your data frame with data.frame or things all get coerced to characters:

    data<-data.frame(LAT=LAT,LON=LON,COLOR=COLOR)
    

    Split the data frame up – you could probably do this in one go but this makes things a bit more obvious:

    query = data[is.na(data$COLOR),]
    colours = data[!is.na(data$COLOR),]
    library(FNN)
    neighs = get.knnx(colours[,c("LAT","LON")],query[,c("LAT","LON")],k=1)
    

    Now insert the replacement colours directly into the data dataframe:

    data[is.na(data$COLOR),"COLOR"]=colours$COLOR[neighs$nn.index]
    plot(data$LON,data$LAT,col=data$COLOR,pch=19)
    

    Note however that distance is being computed using pythagoras geometry on lat-long, which isn’t true because the earth isn’t flat. You might have to transform your coordinates to something else first.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's an example of my problem on jsFiddle. I have a table with striped
Hi I have a code example available here: http://jsbin.com/oxoweh My problem is that I
Here's the problem. I ,for example,have a string 2500.Its converted from byte array into
This problem is not readily reproducible in a simple example here but was wondering
You can see the example here: http://jsfiddle.net/8EHED/8/ This is a tricky problem because I
Here's a very quick example for my problem: http://img19.imageshack.us/img19/3575/workertool.png (I'm not allowed to post
I've created an example of the problem here: http://jsfiddle.net/JustinN/qWeLT/1/ My actual code is hooked
Here is a (big) example of the massive problem I am having, when this
Im having a problem here. i downloaded jake wharton. He gave an example of
I'm facing a problem here, with HttpListener. When a request of the form http://user:password@example.com/

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.