+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 390 views

## 1 answer to this question.

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.

• 8,690 points

## R programming: Finding the difference between 2 vectors

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

## R Programming: Finding items with exceptional sequence

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

## Finding frequency of observations in R

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

## 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

## R Programming error in 'fert'

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

+1 vote

+1 vote

## R Programming: regexpr error

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

+1 vote