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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:39:12+00:00 2026-06-17T20:39:12+00:00

I have a data frame generated from computing nearest neighbor (K=2) using the RANN

  • 0

I have a data frame generated from computing nearest neighbor (K=2) using the RANN package. I would like to transform this data into a matrix with values of 0,1,2 for each cell with 0 = not neighbor, 1=nearest neighbor, 2=2nd nearest neighbor.

The data frame has two columns, the first column is the ID of the 1st NN, the second column is the ID of the 2nd NN. The rows correspond to the ID of the point from which the NN were calculated.

Is there an existing routine to easily to this sort of transformation?

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-17T20:39:13+00:00Added an answer on June 17, 2026 at 8:39 pm

    Based on the limited idea you have given, here is, I think, an unpretty solution:

     NNdf <- data.frame(NN1=c(1,2,4),NN2=c(2,3,1)) # make up some data
     NNdf$origin <- rownames(NNdf)
    
     NNdf
    #  NN1 NN2 origin
    #1   1   2      1
    #2   2   3      2
    #3   4   1      3
    
    
     library(reshape2)
     hold <- melt(NNdf, id = "origin")
     hold
    #  origin variable value
    #1      1      NN1     1
    #2      2      NN1     2
    #3      3      NN1     4
    #4      1      NN2     2
    #5      2      NN2     3
    #6      3      NN2     1
    
    
     hold2 <- dcast(hold, origin~value, value.var="variable")
    
     hold2[hold2 == "NN1"]  <- 1
     hold2[hold2 == "NN2"]  <- 2
     hold2[is.na(hold2) ]   <- 0
    
     hold2
    #  origin 1 2 3 4
    #1      1 1 2 0 0
    #2      2 0 1 2 0
    #3      3 2 0 0 1
    

    (this might rely on apply(hold2,1,as.numeric) afterwards)

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

Sidebar

Related Questions

I have a data.frame of a time series of data, I would like to
I have a very big data frame consisting of data like this: PENR ANFDAT
I have a large data frame (named z ) that looks like this: RPos
I have a data frame like below (20,000 rows by 49 cols). Each row
I have a data frame consisting of results from multiple runs of an experiment,
Let's say you have a data frame generated by the following commands: date <-
I would like to generate a dynamic image from a script, and then have
I have a set of data that looks like this, species<-ABC ind<-rep(1:4,each=24) hour<-rep(seq(0,23,by=1),4) depth<-runif(length(ind),1,50)
I have a dataframe generated from Python's Pandas package. How can I generate heatmap
for a given data frame I would like to multiply values of an array

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.