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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:31:01+00:00 2026-05-29T09:31:01+00:00

I have a spatial dataframe with about 3000 points. I want to generate a

  • 0

I have a spatial dataframe with about 3000 points. I want to generate a matrix that provides the k (in this case 30) nearest neighbors for each point.

I can do it using a loop but i feel that there should be an elegant and optimal way for spatial points dataframe class that i do not know of.

  • 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-05-29T09:31:01+00:00Added an answer on May 29, 2026 at 9:31 am

    Probably the fastest is to use RANN package – assuming you have x and y:

    library(RANN)
    m <- as.matrix(nn(data.frame(x=x, y=y, z=rep(0,length(x))), p=30)$nn.idx)
    

    gives you a 3000 x 30 matrix of closest neighbors. It is several orders of magnitude faster than a naive quadratic search.

    Edit: Just for completeness, it doesn’t matter which ANN frontend you pick, with FNN (suggested by Spacedman) this would be

    library(FNN)
    m <- get.knn(data.frame(x=x, y=y), 30)$nn.index
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a solution that uses spatial data to represent a cluster of points
I have multiple sets of points (for different years ~20) I want to generate
I have spatial data - (x, y) points on a plane - which I'm
There have been a decent amount of questions about mysql spatial datatypes, however mine
I have a pointlist=[p1,p2,p3...] where p1 = [x1,y1],p2=[x2,y2] ... I want to use scipy.spatial.Delaunay
I have a spatially enabled database (DB2, in this case). I need to store
I have some spatial data (MapInfo files) that contains self-intersecting polygons. These cause problems
I'm working on a Geo/Spatial search where I'm looking for nearby points. I have
I have a large non-map image that I want to allow people to view
I want to have a Dictionary that maps strings to generic lists of varying

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.