R programming: Finding closest pair

0 votes

I am defining the following variables:

X<- sample(200:1000,10)
Y<- sample (200:1000, 10)
plot(X,Y)

This will create 10 random points, I want to know, how can I find the shortest path?

Mar 5 in Data Analytics by Sophie may
• 9,050 points
45 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

The dist() function will help you find the distance between each pair of points:

set.seed(1)
X<- sample(200:1000,10)
Y<- sample (200:1000, 10)
dat<-data.frame(X,Y)
print(dat)

     X   Y
1  412 364
2  497 341
3  657 748
4  924 506
5  360 813
6  915 596
7  951 770
8  724 987
9  698 501
10 248 815

 dist(dat)
           1         2         3         4         5         6         7         8         9
2   88.05680                                                                                
3  455.50082 437.32025                                                                      
4  531.32664 457.77068 360.35122                                                            
5  452.00111 491.48042 304.02960 642.14095                                                  
6  553.92509 489.64171 299.44616  90.44888 595.91442                                        
7  674.80145 624.62549 294.82198 265.37709 592.56223 177.68511                              
8  696.75893 684.72257 248.21362 520.92322 403.45012 435.15744 314.03503                    
9  317.11985 256.90660 250.37971 226.05530 459.98696 236.88394 369.28309 486.69498          
10 479.89270 535.42226 414.45144 743.27451 112.01786 702.03276 704.43878 506.12251 548.72215

You can then create a matrix that gives you the min distance:

which(as.matrix(dist(dat))==min(dist(dat)),arr.ind=TRUE)

This is the output:

  row col
2   2   1
1   1   2

Hope this helped.

answered Mar 5 by Tyrion anex
• 8,280 points

Related Questions In Data Analytics

0 votes
1 answer

R programming: Finding the difference between 2 vectors

Try this function, it worked for me: f ...READ MORE

answered Dec 27, 2018 in Data Analytics by Sophie may
• 9,050 points
30 views
0 votes
1 answer

R Programming: Finding items with exceptional sequence

Here's a code that will help with ...READ MORE

answered Feb 27 in Data Analytics by Sophie may
• 9,050 points
12 views
0 votes
1 answer

Finding frequency of observations in R

You can use the "dplyr" package to ...READ MORE

answered Mar 26, 2018 in Data Analytics by Bharani
• 4,550 points
37 views
0 votes
1 answer

R function for finding the index of an element in a vector?

Yes, we can find the index of ...READ MORE

answered Apr 13, 2018 in Data Analytics by zombie
• 3,690 points
69 views
0 votes
1 answer

R programming: Unexpected symbol error

Format your code this way: myfunction <- function() ...READ MORE

answered Dec 17, 2018 in Data Analytics by Sophie may
• 9,050 points
94 views
0 votes
1 answer

R Programming error in 'fert'

You're using a factor: fert <- factor(c(50,20,10,10,20,50)) levels(fert) #[1] ...READ MORE

answered Dec 27, 2018 in Data Analytics by Sophie may
• 9,050 points
33 views
0 votes
1 answer

R Programming: Market Basket Analysis Error

The basket.sorted() has less than 5 rules. Refer ...READ MORE

answered Feb 11 in Data Analytics by Sophie may
• 9,050 points
60 views
0 votes
1 answer

R Programming: regexpr error

The below code will help: gregexpr("D", x) # [[1]] # ...READ MORE

answered Feb 21 in Data Analytics by Tyrion anex
• 8,280 points
15 views
0 votes
1 answer

R programming error

Alright, you can either use gsub to match the ...READ MORE

answered Dec 18, 2018 in Data Analytics by Tyrion anex
• 8,280 points
10 views
0 votes
1 answer

Check if a matrix is diagonalizable in R Programming Language

On a given matrix, a, the first way ...READ MORE

answered Dec 24, 2018 in Data Analytics by Tyrion anex
• 8,280 points
19 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.