Step 01
Genrate two vectors using rnorm(). Each vector should have the same quantity of observations but different mean and SD. Store the vectors in a data.frame named distances where variables vector01 and vector02 correspond to the two vectors previously generated.
a = c(11,20,15,21,34,87,94,12,35,65)
b = c(45,12,35,45,78,98,12,10,22,32)
vector01 = rnorm(a, mean=a, sd=1)
vector02 = rnorm(b, mean=b, sd=5)
distances <- data.frame(vector01,vector02)
Step 02
Print the formulas for euclidean, manhattan, canberra and mahalanobis distances (with code).
EUCLIDEAN
\(d(p,q) = \sqrt{(p_1^2-q_1^2)+(p_2^2-q_2^2)+...+(p_i^2-q_i^2)+...+(p_n^2-n_1^2)}\)
MANHATTAN
\(d(i,n) = \sum_{i=1}^{m}|x_{i}-y_{i}|\)
CANBERRA
\(d(x,y) = \sum_{i=1}^{n}\frac{|x_{i}-y_{i}|}{|x_{i}+y_{i}|}\)
MAHALANOBIS
\(d^2 = (x-\varphi)'\sum^-1(x-\varphi)\)
Step 03
Add 3 columns to the data.frame. • The first column should store the euclidean distance between vector01 and vector02. • The second column should store the Manhattan distance between vector01 and vector02. • The third column should store the Canberra distance between vector01 and vector02. • The fourth column should store the Mahalabobis distance between vector01 and vector02.
distances$euclidean <- as.matrix(dist(distances, method = "euclidean", diag = FALSE, upper = FALSE, p = 2))
distances$manhattan <- as.matrix(dist(distances, method = "manhattan", diag = FALSE, upper = FALSE, p = 2))
distances$canberra <- as.matrix(dist(distances, method = "canberra", diag = FALSE, upper = FALSE, p = 2))
Step 04
Print the head of the data.frame Please do everythink in markdown.
head(distances)
## vector01 vector02 euclidean.1 euclidean.2 euclidean.3 euclidean.4
## 1 11.27243 46.35566 0.000000 25.541479 15.872907 11.038656
## 2 18.66051 21.90605 25.541479 0.000000 9.695708 18.461212
## 3 15.31389 31.00587 15.872907 9.695708 0.000000 10.616241
## 4 20.48648 40.27674 11.038656 18.461212 10.616241 0.000000
## 5 33.63445 86.65076 46.084215 66.453714 58.583239 48.201856
## 6 87.73960 91.95231 89.029678 98.378859 94.657027 84.813609
## euclidean.5 euclidean.6 euclidean.7 euclidean.8 euclidean.9 euclidean.10
## 1 46.084215 89.029678 89.034577 31.960439 32.641611 55.327951
## 2 66.453714 98.378859 75.534607 9.814763 17.365644 46.756266
## 3 58.583239 94.657027 80.455099 16.860971 21.389892 49.170426
## 4 48.201856 84.813609 78.228627 27.122282 21.761300 44.924133
## 5 0.000000 54.364276 95.396840 75.316589 61.888946 63.455228
## 6 54.364276 0.000000 79.709409 108.167607 84.905768 65.052033
## manhattan.1 manhattan.2 manhattan.3 manhattan.4 manhattan.5 manhattan.6
## 1 0.00000 185.73238 126.61980 92.96916 397.36974 681.32027
## 2 185.73238 0.00000 79.34883 130.58887 502.39789 751.13796
## 3 126.61980 79.34883 0.00000 86.60597 484.16081 734.39227
## 4 92.96916 130.58887 86.60597 0.00000 441.40135 702.46627
## 5 397.36974 502.39789 484.16081 441.40135 0.00000 388.84349
## 6 681.32027 751.13796 734.39227 702.46627 388.84349 0.00000
## manhattan.7 manhattan.8 manhattan.9 manhattan.10 canberra.1 canberra.2
## 1 631.21633 193.32898 217.83394 389.24960 0.000000 7.467809
## 2 602.46296 96.45954 144.48726 378.60708 7.467809 0.000000
## 3 632.83579 127.00590 167.99289 384.78329 6.512720 5.946763
## 4 616.43529 191.39847 157.63975 367.99613 5.851632 6.509217
## 5 491.81091 490.53515 464.46935 379.30396 10.408871 11.460126
## 6 412.51709 742.46319 708.91138 547.85760 11.961114 13.051553
## canberra.3 canberra.4 canberra.5 canberra.6 canberra.7 canberra.8
## 1 6.512720 5.851632 10.408871 11.961114 11.891217 6.905138
## 2 5.946763 6.509217 11.460126 13.051553 12.261835 6.022899
## 3 0.000000 5.953808 11.735368 13.098258 12.680447 6.395983
## 4 5.953808 0.000000 11.170527 12.654204 12.302831 7.199738
## 5 11.735368 11.170527 0.000000 7.512856 7.373873 10.679325
## 6 13.098258 12.654204 7.512856 0.000000 6.849195 12.419531
## canberra.9 canberra.10
## 1 7.188734 9.696451
## 2 7.021999 10.700899
## 3 7.527144 10.847660
## 4 6.920832 10.358547
## 5 10.230646 7.311347
## 6 12.178268 9.221143