R programming Finding closest pair

+1 vote

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, 2019 in Data Analytics by Sophie may
• 10,620 points
772 views

1 answer to this question.

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, 2019 by Tyrion anex
• 8,700 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 28, 2018 in Data Analytics by Sophie may
• 10,620 points
1,344 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, 2019 in Data Analytics by Sophie may
• 10,620 points
682 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,660 points
5,893 views
0 votes
2 answers

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

The function match works on vectors : x <- sample(1:10) x # ...READ MORE

answered Dec 12, 2020 in Data Analytics by Rajiv
• 8,870 points
56,640 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
• 10,620 points
3,256 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 28, 2018 in Data Analytics by Sophie may
• 10,620 points
727 views
+1 vote
1 answer

R Programming: Market Basket Analysis Error

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

answered Feb 12, 2019 in Data Analytics by Sophie may
• 10,620 points
1,514 views
+1 vote
1 answer

R Programming: regexpr error

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

answered Feb 21, 2019 in Data Analytics by Tyrion anex
• 8,700 points
630 views
+1 vote
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,700 points
664 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,700 points
1,820 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP