Activity 1.1 - vector basics

SUBMISSION INSTRUCTIONS:

  1. Complete this activity, rendering periodically to html to view your output. Make sure you have this .qmd file in a folder with the Activity subdirectories!
  2. When complete, publish to either Posit Connect Cloud or Rpubs
  3. Submit a link to your rendered html and your .qmd to D2L.

Question 1

Consider the 3-dimensional vector \(w = c(-5, 2, -3)\).

A)

Find \(||w||_1\), the L1 (aka taxicab or Manhattan) norm of this vector. Show your work!

L1 = |-5| + |2| + |-3| = 10

B)

Find \(||w||_2\), the L2 (aka Euclidean) norm of this vector. Show your work!

L2 = sqrt((-5)2 + (2)2 + (-3)2)

= sqrt(25 + 4 + 9)

= sqrt(38)

= 6.16

C)

Create the vector in R. Use R to verify your computations above.

w <- c(-5,2,-3)
L1 <- sum(abs(w))
L1
[1] 10
L2 <- sqrt(sum(w^2))
L2
[1] 6.164414

Question 2

Consider the iris data set, which comes packaged with a standard R installation. In the code below I have selected only the numeric variables from this data set:

library(tidyverse)
(iris_use <- iris 
  %>% select(-Species)
) %>% head()
  Sepal.Length Sepal.Width Petal.Length Petal.Width
1          5.1         3.5          1.4         0.2
2          4.9         3.0          1.4         0.2
3          4.7         3.2          1.3         0.2
4          4.6         3.1          1.5         0.2
5          5.0         3.6          1.4         0.2
6          5.4         3.9          1.7         0.4

A)

How many vectors are there in this new data set, and what is the dimension of each vector?

There are 150 4-dimensional vectors in this data set. ## B)

B)

Find the mean and standard deviation vectors.

(mean_vector <- iris_use
  %>% summarise(across(.cols = c(1:4), .fns = mean))
)
  Sepal.Length Sepal.Width Petal.Length Petal.Width
1     5.843333    3.057333        3.758    1.199333
(sd_vector <- iris_use
  %>% summarise(across(.cols = c(1:4), .fns = sd))
)
  Sepal.Length Sepal.Width Petal.Length Petal.Width
1    0.8280661   0.4358663     1.765298   0.7622377

C)

Mean-center and scale the data set. Verify that the mean and standard deviation vectors of the scaled data set are \(\vec 0\) and \(\vec 1\), respectively.

(iris_scaled <- scale(iris_use, center = TRUE, scale = TRUE))
       Sepal.Length Sepal.Width Petal.Length   Petal.Width
  [1,]  -0.89767388  1.01560199  -1.33575163 -1.3110521482
  [2,]  -1.13920048 -0.13153881  -1.33575163 -1.3110521482
  [3,]  -1.38072709  0.32731751  -1.39239929 -1.3110521482
  [4,]  -1.50149039  0.09788935  -1.27910398 -1.3110521482
  [5,]  -1.01843718  1.24503015  -1.33575163 -1.3110521482
  [6,]  -0.53538397  1.93331463  -1.16580868 -1.0486667950
  [7,]  -1.50149039  0.78617383  -1.33575163 -1.1798594716
  [8,]  -1.01843718  0.78617383  -1.27910398 -1.3110521482
  [9,]  -1.74301699 -0.36096697  -1.33575163 -1.3110521482
 [10,]  -1.13920048  0.09788935  -1.27910398 -1.4422448248
 [11,]  -0.53538397  1.47445831  -1.27910398 -1.3110521482
 [12,]  -1.25996379  0.78617383  -1.22245633 -1.3110521482
 [13,]  -1.25996379 -0.13153881  -1.33575163 -1.4422448248
 [14,]  -1.86378030 -0.13153881  -1.50569459 -1.4422448248
 [15,]  -0.05233076  2.16274279  -1.44904694 -1.3110521482
 [16,]  -0.17309407  3.08045544  -1.27910398 -1.0486667950
 [17,]  -0.53538397  1.93331463  -1.39239929 -1.0486667950
 [18,]  -0.89767388  1.01560199  -1.33575163 -1.1798594716
 [19,]  -0.17309407  1.70388647  -1.16580868 -1.1798594716
 [20,]  -0.89767388  1.70388647  -1.27910398 -1.1798594716
 [21,]  -0.53538397  0.78617383  -1.16580868 -1.3110521482
 [22,]  -0.89767388  1.47445831  -1.27910398 -1.0486667950
 [23,]  -1.50149039  1.24503015  -1.56234224 -1.3110521482
 [24,]  -0.89767388  0.55674567  -1.16580868 -0.9174741184
 [25,]  -1.25996379  0.78617383  -1.05251337 -1.3110521482
 [26,]  -1.01843718 -0.13153881  -1.22245633 -1.3110521482
 [27,]  -1.01843718  0.78617383  -1.22245633 -1.0486667950
 [28,]  -0.77691058  1.01560199  -1.27910398 -1.3110521482
 [29,]  -0.77691058  0.78617383  -1.33575163 -1.3110521482
 [30,]  -1.38072709  0.32731751  -1.22245633 -1.3110521482
 [31,]  -1.25996379  0.09788935  -1.22245633 -1.3110521482
 [32,]  -0.53538397  0.78617383  -1.27910398 -1.0486667950
 [33,]  -0.77691058  2.39217095  -1.27910398 -1.4422448248
 [34,]  -0.41462067  2.62159911  -1.33575163 -1.3110521482
 [35,]  -1.13920048  0.09788935  -1.27910398 -1.3110521482
 [36,]  -1.01843718  0.32731751  -1.44904694 -1.3110521482
 [37,]  -0.41462067  1.01560199  -1.39239929 -1.3110521482
 [38,]  -1.13920048  1.24503015  -1.33575163 -1.4422448248
 [39,]  -1.74301699 -0.13153881  -1.39239929 -1.3110521482
 [40,]  -0.89767388  0.78617383  -1.27910398 -1.3110521482
 [41,]  -1.01843718  1.01560199  -1.39239929 -1.1798594716
 [42,]  -1.62225369 -1.73753594  -1.39239929 -1.1798594716
 [43,]  -1.74301699  0.32731751  -1.39239929 -1.3110521482
 [44,]  -1.01843718  1.01560199  -1.22245633 -0.7862814418
 [45,]  -0.89767388  1.70388647  -1.05251337 -1.0486667950
 [46,]  -1.25996379 -0.13153881  -1.33575163 -1.1798594716
 [47,]  -0.89767388  1.70388647  -1.22245633 -1.3110521482
 [48,]  -1.50149039  0.32731751  -1.33575163 -1.3110521482
 [49,]  -0.65614727  1.47445831  -1.27910398 -1.3110521482
 [50,]  -1.01843718  0.55674567  -1.33575163 -1.3110521482
 [51,]   1.39682886  0.32731751   0.53362088  0.2632599711
 [52,]   0.67224905  0.32731751   0.42032558  0.3944526477
 [53,]   1.27606556  0.09788935   0.64691619  0.3944526477
 [54,]  -0.41462067 -1.73753594   0.13708732  0.1320672944
 [55,]   0.79301235 -0.59039513   0.47697323  0.3944526477
 [56,]  -0.17309407 -0.59039513   0.42032558  0.1320672944
 [57,]   0.55148575  0.55674567   0.53362088  0.5256453243
 [58,]  -1.13920048 -1.50810778  -0.25944625 -0.2615107354
 [59,]   0.91377565 -0.36096697   0.47697323  0.1320672944
 [60,]  -0.77691058 -0.81982329   0.08043967  0.2632599711
 [61,]  -1.01843718 -2.42582042  -0.14615094 -0.2615107354
 [62,]   0.06843254 -0.13153881   0.25038262  0.3944526477
 [63,]   0.18919584 -1.96696410   0.13708732 -0.2615107354
 [64,]   0.30995914 -0.36096697   0.53362088  0.2632599711
 [65,]  -0.29385737 -0.36096697  -0.08950329  0.1320672944
 [66,]   1.03453895  0.09788935   0.36367793  0.2632599711
 [67,]  -0.29385737 -0.13153881   0.42032558  0.3944526477
 [68,]  -0.05233076 -0.81982329   0.19373497 -0.2615107354
 [69,]   0.43072244 -1.96696410   0.42032558  0.3944526477
 [70,]  -0.29385737 -1.27867961   0.08043967 -0.1303180588
 [71,]   0.06843254  0.32731751   0.59026853  0.7880306775
 [72,]   0.30995914 -0.59039513   0.13708732  0.1320672944
 [73,]   0.55148575 -1.27867961   0.64691619  0.3944526477
 [74,]   0.30995914 -0.59039513   0.53362088  0.0008746178
 [75,]   0.67224905 -0.36096697   0.30703027  0.1320672944
 [76,]   0.91377565 -0.13153881   0.36367793  0.2632599711
 [77,]   1.15530226 -0.59039513   0.59026853  0.2632599711
 [78,]   1.03453895 -0.13153881   0.70356384  0.6568380009
 [79,]   0.18919584 -0.36096697   0.42032558  0.3944526477
 [80,]  -0.17309407 -1.04925145  -0.14615094 -0.2615107354
 [81,]  -0.41462067 -1.50810778   0.02379201 -0.1303180588
 [82,]  -0.41462067 -1.50810778  -0.03285564 -0.2615107354
 [83,]  -0.05233076 -0.81982329   0.08043967  0.0008746178
 [84,]   0.18919584 -0.81982329   0.76021149  0.5256453243
 [85,]  -0.53538397 -0.13153881   0.42032558  0.3944526477
 [86,]   0.18919584  0.78617383   0.42032558  0.5256453243
 [87,]   1.03453895  0.09788935   0.53362088  0.3944526477
 [88,]   0.55148575 -1.73753594   0.36367793  0.1320672944
 [89,]  -0.29385737 -0.13153881   0.19373497  0.1320672944
 [90,]  -0.41462067 -1.27867961   0.13708732  0.1320672944
 [91,]  -0.41462067 -1.04925145   0.36367793  0.0008746178
 [92,]   0.30995914 -0.13153881   0.47697323  0.2632599711
 [93,]  -0.05233076 -1.04925145   0.13708732  0.0008746178
 [94,]  -1.01843718 -1.73753594  -0.25944625 -0.2615107354
 [95,]  -0.29385737 -0.81982329   0.25038262  0.1320672944
 [96,]  -0.17309407 -0.13153881   0.25038262  0.0008746178
 [97,]  -0.17309407 -0.36096697   0.25038262  0.1320672944
 [98,]   0.43072244 -0.36096697   0.30703027  0.1320672944
 [99,]  -0.89767388 -1.27867961  -0.42938920 -0.1303180588
[100,]  -0.17309407 -0.59039513   0.19373497  0.1320672944
[101,]   0.55148575  0.55674567   1.27004036  1.7063794137
[102,]  -0.05233076 -0.81982329   0.76021149  0.9192233541
[103,]   1.51759216 -0.13153881   1.21339271  1.1816087073
[104,]   0.55148575 -0.36096697   1.04344975  0.7880306775
[105,]   0.79301235 -0.13153881   1.15674505  1.3128013839
[106,]   2.12140867 -0.13153881   1.60992627  1.1816087073
[107,]  -1.13920048 -1.27867961   0.42032558  0.6568380009
[108,]   1.75911877 -0.36096697   1.43998331  0.7880306775
[109,]   1.03453895 -1.27867961   1.15674505  0.7880306775
[110,]   1.63835547  1.24503015   1.32668801  1.7063794137
[111,]   0.79301235  0.32731751   0.76021149  1.0504160307
[112,]   0.67224905 -0.81982329   0.87350679  0.9192233541
[113,]   1.15530226 -0.13153881   0.98680210  1.1816087073
[114,]  -0.17309407 -1.27867961   0.70356384  1.0504160307
[115,]  -0.05233076 -0.59039513   0.76021149  1.5751867371
[116,]   0.67224905  0.32731751   0.87350679  1.4439940605
[117,]   0.79301235 -0.13153881   0.98680210  0.7880306775
[118,]   2.24217198  1.70388647   1.66657392  1.3128013839
[119,]   2.24217198 -1.04925145   1.77986923  1.4439940605
[120,]   0.18919584 -1.96696410   0.70356384  0.3944526477
[121,]   1.27606556  0.32731751   1.10009740  1.4439940605
[122,]  -0.29385737 -0.59039513   0.64691619  1.0504160307
[123,]   2.24217198 -0.59039513   1.66657392  1.0504160307
[124,]   0.55148575 -0.81982329   0.64691619  0.7880306775
[125,]   1.03453895  0.55674567   1.10009740  1.1816087073
[126,]   1.63835547  0.32731751   1.27004036  0.7880306775
[127,]   0.43072244 -0.59039513   0.59026853  0.7880306775
[128,]   0.30995914 -0.13153881   0.64691619  0.7880306775
[129,]   0.67224905 -0.59039513   1.04344975  1.1816087073
[130,]   1.63835547 -0.13153881   1.15674505  0.5256453243
[131,]   1.87988207 -0.59039513   1.32668801  0.9192233541
[132,]   2.48369858  1.70388647   1.49663097  1.0504160307
[133,]   0.67224905 -0.59039513   1.04344975  1.3128013839
[134,]   0.55148575 -0.59039513   0.76021149  0.3944526477
[135,]   0.30995914 -1.04925145   1.04344975  0.2632599711
[136,]   2.24217198 -0.13153881   1.32668801  1.4439940605
[137,]   0.55148575  0.78617383   1.04344975  1.5751867371
[138,]   0.67224905  0.09788935   0.98680210  0.7880306775
[139,]   0.18919584 -0.13153881   0.59026853  0.7880306775
[140,]   1.27606556  0.09788935   0.93015445  1.1816087073
[141,]   1.03453895  0.09788935   1.04344975  1.5751867371
[142,]   1.27606556  0.09788935   0.76021149  1.4439940605
[143,]  -0.05233076 -0.81982329   0.76021149  0.9192233541
[144,]   1.15530226  0.32731751   1.21339271  1.4439940605
[145,]   1.03453895  0.55674567   1.10009740  1.7063794137
[146,]   1.03453895 -0.13153881   0.81685914  1.4439940605
[147,]   0.55148575 -1.27867961   0.70356384  0.9192233541
[148,]   0.79301235 -0.13153881   0.81685914  1.0504160307
[149,]   0.43072244  0.78617383   0.93015445  1.4439940605
[150,]   0.06843254 -0.13153881   0.76021149  0.7880306775
attr(,"scaled:center")
Sepal.Length  Sepal.Width Petal.Length  Petal.Width 
    5.843333     3.057333     3.758000     1.199333 
attr(,"scaled:scale")
Sepal.Length  Sepal.Width Petal.Length  Petal.Width 
   0.8280661    0.4358663    1.7652982    0.7622377 
(iris_scaled 
  %>% data.frame()
  %>% summarise(across(everything(.), \(x) round(mean(x),1)))
)
  Sepal.Length Sepal.Width Petal.Length Petal.Width
1            0           0            0           0
(iris_scaled
  %>% data.frame()
  %>% summarise(across(everything(.), \(x) round(sd(x),1)))
)
  Sepal.Length Sepal.Width Petal.Length Petal.Width
1            1           1            1           1

D)

Find the centered/scaled vectors for the first two irises. Find the L1 and L2 distances between these two irises “from scratch,” then verify your answer using dist.

(iris_1and2 <- iris_scaled[1:2,])
     Sepal.Length Sepal.Width Petal.Length Petal.Width
[1,]   -0.8976739   1.0156020    -1.335752   -1.311052
[2,]   -1.1392005  -0.1315388    -1.335752   -1.311052

L1 = |-1.139 + 0.898| + |-0.132 - 1.016| + |-1.336 + 1.336| + |-1.311 + 1.311|

= 0.241 + 1.148 + 0 + 0

= 1.389

L2 = sqrt[(-1.139 + 0.898)2 + (-0.132 - 1.016)2 + (-1.336 + 1.366)2 + (-1.311 + 1.311)2]

= sqrt(0.058 + 1.318 + 0 + 0)

= sqrt(1.376)

= 1.173

(L1_dist <- dist(iris_1and2, method = 'manhattan'))
         1
2 1.388667
(L2_dist <- dist(iris_1and2, method = 'euclidean'))
         1
2 1.172291

Question 3

Reconsider the USairpollution data. If you don’t have the HSAUR2 package installed, run install.packages(HSAUR2) once in your console to install the package. Do not insert this code in your .qmd, as you only need to install the package once, not every time you compile the html.

library(HSAUR2)
data("USairpollution")
head(USairpollution)
            SO2 temp manu popul wind precip predays
Albany       46 47.6   44   116  8.8  33.36     135
Albuquerque  11 56.8   46   244  8.9   7.77      58
Atlanta      24 61.5  368   497  9.1  48.34     115
Baltimore    47 55.0  625   905  9.6  41.31     111
Buffalo      11 47.1  391   463 12.4  36.11     166
Charleston   31 55.2   35    71  6.5  40.75     148

A)

GOAL: Find the cities that are most and least similar with respect to their pollution. Use dist to find L2 distances between the cities. Then use wrangling approaches (i.e., DSCI 210/325 techniques) to find the two cities that are most similar, and two cities that are most dissimilar.

(pollution_dist <- dist(USairpollution, method = 'euclidean'))
                   Albany Albuquerque    Atlanta  Baltimore    Buffalo
Albuquerque     155.82522                                             
Atlanta         501.43664   415.66700                                 
Baltimore       980.19264   881.70044  482.85600                      
Buffalo         492.97543   423.74461   69.44655  504.51788           
Charleston       51.16309   199.11303  541.85520 1022.39472  530.32627
Chicago        4634.25147  4545.00583 4136.75219 3669.93381 4144.49044
Cincinnati      537.50080   472.59485  105.87134  481.56246   80.77924
Cleveland      1153.84855  1092.56137  690.24539  414.69232  682.23696
Columbus        479.05070   378.02137  113.34293  512.94029  151.12118
Dallas          944.12289   845.76292  443.52124   81.64537  464.51705
Denver          575.30696   490.68916   99.77285  428.57935  116.96147
Des Moines      112.71211    88.72889  397.45636  876.45249  393.76480
Detroit        1729.79153  1628.76911 1231.83872  750.32418 1247.96380
Hartford        370.74855   386.60660  343.80732  777.01635  311.57707
Houston        1307.29324  1198.98115  816.51863  344.20195  840.47232
Indianapolis    705.66341   597.04860  249.55793  308.95292  288.69404
Jacksonville    425.78900   308.24253  234.59386  618.03267  269.61777
Kansas City     518.49040   428.88797   28.34441  468.17944   81.21153
Little Rock      72.03295   134.22255  458.58597  940.24037  451.97993
Louisville      537.67770   433.20055  123.72471  457.53502  171.02064
Memphis         588.68744   482.72383  131.85825  404.24548  181.51647
Miami           278.12322   205.32857  229.90725  708.55098  230.35698
Milwaukee       798.68706   708.58543  299.26120  199.56490  313.25051
Minneapolis     907.62168   826.73087  414.61670  181.01516  418.46102
Nashville       406.10937   315.11314  105.41976  576.45316  127.31203
New Orleans     297.41331   210.27482  213.86630  689.23315  221.53617
Norfolk         201.05291   108.31199  331.32384  797.84826  337.89869
Omaha           273.04346   176.00369  241.43534  714.06990  249.49103
Philadelphia   2465.86330  2372.21512 1966.29819 1493.65862 1977.32488
Phoenix         507.96589   377.90102  198.69301  531.49581  253.31736
Pittsburgh      505.38142   421.95807   60.27599  476.49565   89.81037
Providence      309.63070   324.16775  326.97821  780.40571  302.63136
Richmond        240.60065   174.68578  261.72123  742.21997  259.87982
Salt Lake City  122.60280   119.34588  397.87518  878.14014  391.88057
San Francisco   730.27742   623.44302  241.70929  262.48071  279.28400
Seattle         534.43624   453.66616   62.46961  451.16139   71.61133
St. Louis       889.64756   824.22746  427.29910  320.52488  422.57413
Washington      750.97997   646.38600  268.52185  242.32259  302.91150
Wichita         191.93472    91.92266  330.37310  804.28434  335.46672
Wilmington       56.75801   181.31685  507.05001  988.82884  496.80510
               Charleston    Chicago Cincinnati  Cleveland   Columbus
Albuquerque                                                          
Atlanta                                                              
Baltimore                                                            
Buffalo                                                              
Charleston                                                           
Chicago        4672.60936                                            
Cincinnati      573.21700 4100.81609                                 
Cleveland      1186.79054 3509.78751  623.03397                      
Columbus        523.04406 4181.45138  214.50086  771.73610           
Dallas          985.04441 3700.56980  433.82818  389.79886  486.50645
Denver          614.43224 4062.98246   82.15052  607.50655  197.31499
Des Moines      155.05012 4532.42210  438.92355 1059.68614  377.17099
Detroit        1771.64299 2940.88808 1219.11775  765.17052 1258.44365
Hartford        388.34820 4348.58252  301.14012  840.60288  410.16505
Houston        1350.39532 3384.30349  822.68489  566.03170  830.01421
Indianapolis    750.10779 3973.05485  310.17050  647.98786  227.24182
Jacksonville    470.79270 4285.65475  335.91269  901.54344  134.46004
Kansas City     559.04276 4120.70139  103.24641  676.14857  125.27554
Little Rock      97.88719 4590.24860  491.88015 1108.27074  445.68699
Louisville      581.94069 4127.16496  221.34038  734.83094   60.20889
Memphis         631.99760 4072.80006  214.30673  686.21978  115.94196
Miami           317.65113 4365.45829  282.89722  904.48592  216.54917
Milwaukee       838.79483 3839.61206  285.42141  443.25710  351.37718
Minneapolis     945.68654 3727.40643  375.77268  310.89135  478.86694
Nashville       448.09169 4237.88772  187.79308  794.58310   94.75083
New Orleans     338.82423 4349.57031  275.87231  895.89931  193.10033
Norfolk         246.90700 4463.81928  394.17401 1014.41271  288.43833
Omaha           316.90026 4375.71299  302.54452  922.69625  214.39669
Philadelphia   2505.76009 2178.17396 1938.12316 1381.48185 2005.94246
Phoenix         554.21471 4193.93415  298.85993  823.05043  125.26293
Pittsburgh      547.62941 4135.43712  139.31691  699.31771   91.41922
Providence      333.27744 4379.77573  307.19540  877.42886  375.49194
Richmond        281.69728 4397.24233  307.02816  929.33270  251.54488
Salt Lake City  159.98693 4526.39872  429.89456 1045.77692  389.41398
San Francisco   773.35742 3925.44889  271.92586  564.75941  266.31630
Seattle         574.65212 4105.33971  118.51904  666.46804  117.31568
St. Louis       923.96326 3761.51645  358.28382  270.34646  517.26920
Washington      794.47197 3911.18804  306.09272  575.91018  275.47897
Wichita         235.72193 4464.80517  383.90063 1005.60842  303.58222
Wilmington       57.40157 4634.30731  534.36979 1145.48757  496.70203
                   Dallas     Denver Des Moines    Detroit   Hartford
Albuquerque                                                          
Atlanta                                                              
Baltimore                                                            
Buffalo                                                              
Charleston                                                           
Chicago                                                              
Cincinnati                                                           
Cleveland                                                            
Columbus                                                             
Dallas                                                               
Denver          379.57165                                            
Des Moines      838.34888  470.87011                                 
Detroit         793.76201 1170.72908 1626.02153                      
Hartford        726.63486  365.16875  314.59752 1503.90487           
Houston         398.12606  767.25817 1202.68817  444.98131 1119.91808
Indianapolis    300.69335  253.02524  602.98789 1040.51788  590.92907
Jacksonville    596.71736  322.83928  331.24919 1353.12478  464.97069
Kansas City     426.34941   78.46759  412.86743 1216.52113  354.09142
Little Rock     900.07588  529.16791   73.62220 1689.86691  326.28787
Louisville      433.74823  187.38945  435.20602 1201.73764  452.32821
Memphis         376.48803  165.47191  483.49439 1149.13783  474.94223
Miami           671.26552  313.00780  175.45425 1457.47978  276.47406
Milwaukee       154.45147  236.01935  694.90749  937.58312  582.21121
Minneapolis     133.64273  339.78355  806.36761  851.32392  653.40013
Nashville       541.00873  196.90039  301.42258 1325.69087  323.25101
New Orleans     652.63125  298.66120  191.87542 1438.27800  295.67983
Norfolk         765.75002  416.14074  110.34364 1545.80427  351.01327
Omaha           677.70441  321.25791  165.18411 1463.09631  303.10121
Philadelphia   1527.40243 1896.35165 2362.98738  766.03381 2202.27597
Phoenix         504.44866  255.93235  403.27059 1265.40138  481.19807
Pittsburgh      446.23869  132.94699  405.85224 1225.22096  368.43856
Providence      735.37195  365.46027  253.30016 1517.57480   81.57055
Richmond        704.23339  338.44098  136.77071 1491.96713  259.28413
Salt Lake City  837.48986  464.26913   47.82376 1627.51329  281.02658
San Francisco   228.44380  202.16472  623.31082 1006.50747  564.93769
Seattle         417.90834  113.03763  434.12060 1197.86194  377.28124
St. Louis       265.11517  341.92761  793.14747  937.27246  589.61974
Washington      228.02954  245.83558  646.82389  984.33444  600.29776
Wichita         766.73733  406.60619   82.45655 1553.19245  317.95051
Wilmington      949.00634  575.32226  125.76645 1738.41172  341.92213
                  Houston Indianapolis Jacksonville Kansas City Little Rock
Albuquerque                                                                
Atlanta                                                                    
Baltimore                                                                  
Buffalo                                                                    
Charleston                                                                 
Chicago                                                                    
Cincinnati                                                                 
Cleveland                                                                  
Columbus                                                                   
Dallas                                                                     
Denver                                                                     
Des Moines                                                                 
Detroit                                                                    
Hartford                                                                   
Houston                                                                    
Indianapolis    606.45121                                                  
Jacksonville    915.46146    313.75540                                     
Kansas City     801.89851    241.26533    247.58403                        
Little Rock    1268.53785    671.36844    399.97690   474.24171            
Louisville      771.69226    168.37146    169.46722   127.94308   503.38597
Memphis         719.99839    127.41299    222.79640   125.99389   550.10517
Miami          1035.08772    440.44552    207.16143   248.35225   236.21204
Milwaukee       539.16678    210.69997    473.34496   283.13877   756.22242
Minneapolis     492.20145    338.74340    604.39822   399.00587   864.09193
Nashville       903.08607    310.50017    161.43281   123.46020   366.20623
New Orleans    1013.83122    416.99133    181.34900   231.19224   255.96170
Norfolk        1116.67566    512.04306    225.63664   348.54897   177.77625
Omaha          1037.91335    438.63579    190.66469   256.23685   234.01687
Philadelphia   1208.64766   1795.24392   2108.03356  1950.45117  2423.18029
Phoenix         829.50075    240.18940    131.96252   197.47568   472.54639
Pittsburgh      808.25088    230.31945    220.06686    76.52306   469.95541
Providence     1123.26615    571.14550    415.10138   340.81912   270.29438
Richmond       1071.21278    476.22944    238.79057   278.51014   198.90894
Salt Lake City 1208.40396    613.72489    356.90651   412.16925    74.90309
San Francisco   584.22954    113.50439    373.01879   223.97885   688.46563
Seattle         783.74560    220.00121    249.25391    71.02904   496.68010
St. Louis       615.36898    433.39719    647.57776   412.63112   842.62519
Washington      556.41109     74.67384    376.03749   256.30041   713.27319
Wichita        1126.97028    526.94599    256.01207   344.69395   151.29922
Wilmington     1319.61180    722.93545    453.46952   523.11644    60.56429
               Louisville    Memphis      Miami  Milwaukee Minneapolis
Albuquerque                                                           
Atlanta                                                               
Baltimore                                                             
Buffalo                                                               
Charleston                                                            
Chicago                                                               
Cincinnati                                                            
Cleveland                                                             
Columbus                                                              
Dallas                                                                
Denver                                                                
Des Moines                                                            
Detroit                                                               
Hartford                                                              
Houston                                                               
Indianapolis                                                          
Jacksonville                                                          
Kansas City                                                           
Little Rock                                                           
Louisville                                                            
Memphis          62.23898                                             
Miami           273.34970  318.21021                                  
Milwaukee       305.22677  251.97970  528.08069                       
Minneapolis     435.78493  384.31101  641.84115  134.20163            
Nashville       146.50581  187.33964  134.12256  399.13221   518.16938
New Orleans     249.56341  295.00400   31.18302  511.27254   627.90156
Norfolk         345.42936  398.15297  118.86385  625.87065   744.81597
Omaha           271.45489  318.73636   56.50623  536.75756   654.03445
Philadelphia   1950.85822 1896.84350 2194.97881 1668.68001  1562.97439
Phoenix         125.28250  154.12700  268.93792  392.03275   523.54392
Pittsburgh      100.38786  124.78932  240.77622  301.28553   418.75856
Providence      422.18098  453.53483  225.47841  588.91007   671.30829
Richmond        308.80026  354.45787   49.31971  560.01682   671.57480
Salt Lake City  446.72918  492.49148  186.05961  693.42333   800.59281
San Francisco   212.92809  155.56701  459.66017  129.65469   258.25323
Seattle         115.36950  120.29791  265.88332  269.58375   385.64533
St. Louis       485.95335  440.64848  639.21366  231.38894   150.55555
Washington      217.97178  166.18587  480.65742  142.74187   267.26360
Wichita         360.21433  407.75758  115.89956  626.58032   742.44767
Wilmington      554.81369  602.39366  287.87229  803.47540   908.26901
                Nashville New Orleans    Norfolk      Omaha Philadelphia
Albuquerque                                                             
Atlanta                                                                 
Baltimore                                                               
Buffalo                                                                 
Charleston                                                              
Chicago                                                                 
Cincinnati                                                              
Cleveland                                                               
Columbus                                                                
Dallas                                                                  
Denver                                                                  
Des Moines                                                              
Detroit                                                                 
Hartford                                                                
Houston                                                                 
Indianapolis                                                            
Jacksonville                                                            
Kansas City                                                             
Little Rock                                                             
Louisville                                                              
Memphis                                                                 
Miami                                                                   
Milwaukee                                                               
Minneapolis                                                             
Nashville                                                               
New Orleans     113.67697                                               
Norfolk         227.65773   123.28020                                   
Omaha           140.76819    44.39052   98.13348                        
Philadelphia   2065.56839  2177.87551 2290.16826 2203.66748             
Phoenix         174.33451   239.44485  311.71169  247.02519   2017.40614
Pittsburgh      114.80374   224.35176  331.59949  249.26881   1963.42671
Providence      287.92918   245.66296  285.98989  248.51025   2226.43147
Richmond        168.46150    67.06685  101.60521   56.54722   2227.71100
Salt Lake City  308.30130   204.15910  144.17469  178.00110   2359.67897
San Francisco   326.95267   437.70947  545.21769  459.60038   1750.37326
Seattle         141.32984   251.28892  363.63357  278.77866   1934.30726
St. Louis       531.06781   630.09995  748.64747  655.99384   1613.92689
Washington      347.85618   458.86981  562.06158  482.30184   1734.19649
Wichita         231.25453   122.88326   60.80715   91.41887   2293.32271
Wilmington      416.96694   309.10315  228.73407  286.95168   2469.11620
                  Phoenix Pittsburgh Providence   Richmond Salt Lake City
Albuquerque                                                              
Atlanta                                                                  
Baltimore                                                                
Buffalo                                                                  
Charleston                                                               
Chicago                                                                  
Cincinnati                                                               
Cleveland                                                                
Columbus                                                                 
Dallas                                                                   
Denver                                                                   
Des Moines                                                               
Detroit                                                                  
Hartford                                                                 
Houston                                                                  
Indianapolis                                                             
Jacksonville                                                             
Kansas City                                                              
Little Rock                                                              
Louisville                                                               
Memphis                                                                  
Miami                                                                    
Milwaukee                                                                
Minneapolis                                                              
Nashville                                                                
New Orleans                                                              
Norfolk                                                                  
Omaha                                                                    
Philadelphia                                                             
Phoenix                                                                  
Pittsburgh      194.88576                                                
Providence      442.71180  343.38643                                     
Richmond        297.09241  271.45640  201.27063                          
Salt Lake City  417.36161  409.06351  221.04811  142.15592               
San Francisco   277.30576  242.35365  557.75754  492.34982      626.30554
Seattle         219.74901   49.64972  361.88994  299.03272      436.77615
St. Louis       570.41420  442.04906  620.32256  662.92165      779.39333
Washington      294.35638  257.12400  588.94472  515.72748      653.34347
Wichita         321.98640  339.77660  258.00984   85.16273      105.25601
Wilmington      527.09575  516.36456  287.19298  248.54150      117.43976
               San Francisco    Seattle  St. Louis Washington    Wichita
Albuquerque                                                             
Atlanta                                                                 
Baltimore                                                               
Buffalo                                                                 
Charleston                                                              
Chicago                                                                 
Cincinnati                                                              
Cleveland                                                               
Columbus                                                                
Dallas                                                                  
Denver                                                                  
Des Moines                                                              
Detroit                                                                 
Hartford                                                                
Houston                                                                 
Indianapolis                                                            
Jacksonville                                                            
Kansas City                                                             
Little Rock                                                             
Louisville                                                              
Memphis                                                                 
Miami                                                                   
Milwaukee                                                               
Minneapolis                                                             
Nashville                                                               
New Orleans                                                             
Norfolk                                                                 
Omaha                                                                   
Philadelphia                                                            
Phoenix                                                                 
Pittsburgh                                                              
Providence                                                              
Richmond                                                                
Salt Lake City                                                          
San Francisco                                                           
Seattle            223.06848                                            
St. Louis          340.78326  411.50755                                 
Washington          67.82369  238.63876  367.80701                      
Wichita            548.32510  369.19581  737.83259  572.05254           
Wilmington         739.46004  543.47190  881.64214  764.01298  206.77514
(distance_df <- pollution_dist
  %>% as.matrix
  %>% data.frame(check.names = FALSE) #handles the issue of replacing spaces with periods
  %>% rownames_to_column('CityA')
  %>% pivot_longer(cols = -CityA,
                   names_to = 'CityB',
                   values_to = 'Distance'
                   )
  %>% filter(CityA != CityB) #remove the distances betwen a city and itself
  
  #remove duplicate rows
  %>% rowwise()
  %>% mutate(City1 = min(CityA, CityB),
             City2 = max(CityA, CityB))
  %>% ungroup()
  %>% distinct(City1, City2, .keep_all = TRUE)
  %>% select(-c(CityA, CityB))
  %>% arrange(desc(Distance))
)
# A tibble: 820 × 3
   Distance City1       City2         
      <dbl> <chr>       <chr>         
 1    4673. Charleston  Chicago       
 2    4634. Chicago     Wilmington    
 3    4634. Albany      Chicago       
 4    4590. Chicago     Little Rock   
 5    4545. Albuquerque Chicago       
 6    4532. Chicago     Des Moines    
 7    4526. Chicago     Salt Lake City
 8    4465. Chicago     Wichita       
 9    4464. Chicago     Norfolk       
10    4397. Chicago     Richmond      
# ℹ 810 more rows
(most_dissimilar <- distance_df[1,])
# A tibble: 1 × 3
  Distance City1      City2  
     <dbl> <chr>      <chr>  
1    4673. Charleston Chicago
# Charleston and Chicago

(most_similar <- distance_df
  %>% arrange(desc(-Distance))
)[1,]
# A tibble: 1 × 3
  Distance City1   City2      
     <dbl> <chr>   <chr>      
1     28.3 Atlanta Kansas City
# Atlanta and Kansas City

B)

Assess how scaling the data before computing distance impacts your answer to the previous question.

(scaled_pollution <- scale(USairpollution, center = FALSE, scale = TRUE))
                     SO2      temp       manu      popul      wind    precip
Albany         1.1971497 0.8363065 0.06002535 0.13718084 0.9102788 0.8544499
Albuquerque    0.2862749 0.9979456 0.06275377 0.28855281 0.9206229 0.1990131
Atlanta        0.6245998 1.0805220 0.50203016 0.58774895 0.9413110 1.2381327
Baltimore      1.2231747 0.9663205 0.85263275 1.07024709 0.9930314 1.0580733
Buffalo        0.2862749 0.8275218 0.53340705 0.54754078 1.2826656 0.9248857
Charleston     0.8067748 0.9698344 0.04774743 0.08396414 0.6723650 1.0437300
Chicago        2.8627493 0.8890149 4.56192625 3.98415740 1.0757840 0.8821120
Cincinnati     0.5985748 0.9487511 0.63026613 0.53571484 0.7344295 0.9999318
Cleveland      1.6916246 0.8732024 1.37376188 0.88812770 1.1275044 0.8961991
Columbus       0.6766498 0.9048274 0.36288050 0.63860047 0.8895907 0.9479374
Dallas         0.2342249 1.1630985 0.87446015 0.99810889 1.1275044 0.9205315
Denver         0.4424249 0.9118552 0.61935243 0.60903564 0.9309670 0.3316884
Des Moines     0.4424249 0.8609037 0.14187809 0.23770129 1.1585367 0.7901613
Detroit        0.9108748 0.8767163 1.45152199 1.78926392 1.0447518 0.7929787
Hartford       1.4573996 0.8626607 0.56205551 0.18684977 0.9309670 1.1108361
Houston        0.2602499 1.2105361 0.98359714 1.45813775 1.1171604 1.2342908
Indianapolis   0.7286998 0.9188830 0.49248068 0.88221473 1.0033755 0.9922479
Jacksonville   0.3643499 1.2017513 0.18553289 0.62559195 0.9102788 1.3951405
Kansas City    0.3643499 0.9575358 0.51976492 0.59957489 1.0344077 0.9476812
Little Rock    0.3383249 1.0717373 0.12414333 0.15610234 0.8482143 1.2427431
Louisville     0.7807498 0.9768622 0.39698581 0.70127793 0.8585584 1.1041767
Memphis        0.2602499 1.0822790 0.45973958 0.73793833 0.9516551 1.2575986
Miami          0.2602499 1.3264945 0.28239197 0.39616881 0.9309670 1.5316578
Milwaukee      0.4163999 0.8029245 0.77623685 0.84791952 1.2206011 0.7445701
Minneapolis    0.7547248 0.7642717 0.95358447 0.87984954 1.0964722 0.6644014
Nashville      0.4684499 1.0436262 0.37515841 0.52980187 0.8171821 1.1781983
New Orleans    0.2342249 1.1999944 0.27829933 0.42691624 0.8689025 1.4540504
Norfolk        0.8067748 1.0418692 0.13096439 0.36423879 1.0964722 1.1443891
Omaha          0.3643499 0.9048274 0.24692244 0.41035993 1.1275044 0.7730005
Philadelphia   1.7957245 0.9592927 2.30824737 2.30605727 0.9930314 1.0227274
Phoenix        0.2602499 1.2351333 0.29057724 0.68826940 0.6206446 0.1805717
Pittsburgh     1.5875246 0.8855010 0.47338170 0.61494860 0.9723433 0.9277031
Providence     2.4463494 0.8784732 0.46792485 0.21168423 1.0964722 1.0949560
Richmond       0.6766498 1.0155150 0.26874984 0.35359545 0.7861499 1.0908580
Salt Lake City 0.7286998 0.8960427 0.18689710 0.20813645 0.8999347 0.3885493
San Francisco  0.3122999 0.9961886 0.61798822 0.84673692 0.8999347 0.5291647
Seattle        0.7547248 0.8977996 0.51703650 0.62795713 0.9723433 0.9935285
St. Louis      1.4573996 0.9821330 1.05726461 0.73557314 0.9826873 0.9192508
Washington     0.7547248 1.0067303 0.59206818 0.89522326 0.9619992 0.9960898
Wichita        0.2081999 0.9944317 0.17052655 0.32757839 1.3136978 0.7832457
Wilmington     0.9368998 0.9487511 0.10913699 0.09460748 0.9309670 1.0309235
                 predays
Albany         1.1409297
Albuquerque    0.4901772
Atlanta        0.9719031
Baltimore      0.9380978
Buffalo        1.4029210
Charleston     1.2507970
Chicago        1.0310624
Cincinnati     1.1155757
Cleveland      1.3099563
Columbus       1.1324784
Dallas         0.6592038
Denver         0.7268145
Des Moines     0.8704871
Detroit        1.0902217
Hartford       1.0733191
Houston        0.8704871
Indianapolis   1.0226111
Jacksonville   0.9803544
Kansas City    0.8366818
Little Rock    0.8451331
Louisville     1.0395137
Memphis        0.8873898
Miami          1.0817704
Milwaukee      1.0395137
Minneapolis    1.1578324
Nashville      1.0057084
New Orleans    0.9550004
Norfolk        0.9803544
Omaha          0.8282305
Philadelphia   0.9719031
Phoenix        0.3042479
Pittsburgh     1.2423457
Providence     1.0564164
Richmond       0.9719031
Salt Lake City 0.7521685
San Francisco  0.5662392
Seattle        1.3860183
St. Louis      0.8873898
Washington     0.9380978
Wichita        0.6930092
Wilmington     0.9634518
attr(,"scaled:scale")
       SO2       temp       manu      popul       wind     precip    predays 
 38.424601  56.916933 733.023687 845.599122   9.667368  39.042663 118.324554 
(pollution_dist <- dist(scaled_pollution, method = 'euclidean'))
                  Albany Albuquerque   Atlanta Baltimore   Buffalo Charleston
Albuquerque    1.3160259                                                     
Atlanta        0.9811116   1.3099703                                         
Baltimore      1.2672298   1.7485984 0.8736702                               
Buffalo        1.1977179   1.3448794 0.7625596 1.2608968                     
Charleston     0.5269780   1.2913223 0.8327770 1.4123771 1.0731608           
Chicago        6.1549629   6.4291048 5.7630433 4.9990364 5.9045923  6.3292994
Cincinnati     0.9527173   1.2451397 0.3967044 0.9095928 0.7145008  0.7818271
Cleveland      1.6165369   2.2955842 1.5157617 0.8458276 1.6831014  1.8519469
Columbus       0.7924629   1.1610661 0.4101208 0.8884034 0.6510902  0.7060108
Dallas         1.6498884   1.3357696 0.8364390 1.0663987 1.0059120  1.5673516
Denver         1.2474012   0.7197309 0.9786064 1.2065654 0.9876022  1.2597542
Des Moines     0.8520111   0.7763405 0.7692941 1.3866246 0.7693095  0.7889284
Detroit        2.1848709   2.3051874 1.6405866 1.0379333 1.7173059  2.2639502
Hartford       0.6274225   1.6796022 0.9653698 0.9774218 1.3306365  0.9015397
Houston        1.9687456   1.8753176 1.0864441 1.0979563 1.2593032  1.8707182
Indianapolis   1.0049935   1.2878842 0.4366325 0.6506949 0.7382036  1.0039774
Jacksonville   1.1830189   1.3591924 0.4579465 1.2481971 0.9009028  0.9028301
Kansas City    1.1180732   1.0034137 0.4411620 1.0460278 0.6392741  0.9981672
Little Rock    1.0196058   1.1180677 0.6602323 1.4913573 0.9919576  0.6899001
Louisville     0.8367893   1.2852808 0.2973461 0.7550477 0.8081651  0.7659849
Memphis        1.2990335   1.2832298 0.4058666 1.1178364 0.7702564  1.0791481
Miami          1.3027377   1.5147526 0.6136220 1.4440504 0.9646633  0.9549577
Milwaukee      1.3219121   1.2515211 0.7677655 0.9450964 0.5789316  1.3150195
Minneapolis    1.2735798   1.4527672 0.8910573 0.7215763 0.8203734  1.3532698
Nashville      0.9793926   1.1942983 0.2556195 1.0703143 0.7374728  0.7245691
New Orleans    1.2597165   1.3794896 0.5428978 1.3960122 0.9343037  0.9198058
Norfolk        0.6290894   1.2033965 0.5052352 1.1037597 0.8796800  0.5948162
Omaha          0.9794946   0.7416626 0.6832300 1.2865138 0.7002693  0.8973179
Philadelphia   3.1930675   3.5083195 2.7659190 1.9940651 2.9694238  3.3476470
Phoenix        1.6222617   0.6273029 1.3705831 1.6648557 1.5636345  1.5610562
Pittsburgh     0.7572591   1.7551255 1.0664364 0.7748561 1.3516107  1.0878015
Providence     1.3542025   2.4504145 1.8851790 1.5540474 2.2291843  1.7634307
Richmond       0.7023559   1.1157492 0.4024271 1.0957353 0.8690474  0.4832310
Salt Lake City 0.7825922   0.5770561 1.0295976 1.3883019 1.1372413  0.8809100
San Francisco  1.4350960   0.8577580 0.9241619 1.1671717 1.0625030  1.3918986
Seattle        0.8558651   1.4097885 0.5333362 0.8588437 0.5765558  0.7967752
St. Louis      1.2309669   1.8002873 1.0695388 0.4805645 1.4356224  1.4529429
Washington     1.0706818   1.3059179 0.4303261 0.5695704 0.8365661  1.0688477
Wichita        1.1916110   0.7458033 0.8841718 1.5120235 0.8596627  1.1061675
Wilmington     0.3842754   1.1754445 0.7453606 1.2618053 1.0729548  0.4133585
                 Chicago Cincinnati Cleveland  Columbus    Dallas    Denver
Albuquerque                                                                
Atlanta                                                                    
Baltimore                                                                  
Buffalo                                                                    
Charleston                                                                 
Chicago                                                                    
Cincinnati     5.7111565                                                   
Cleveland      4.6045880  1.4423932                                        
Columbus       5.8011467  0.3423005 1.4852936                              
Dallas         5.4443167  0.9062132 1.7010357 0.9668230                    
Denver         5.7628406  0.8170377 1.7048090 0.8170683 0.8443119          
Des Moines     6.2827229  0.8021059 1.9252490 0.6574325 1.1454978 0.8069040
Detroit        4.2795496  1.5774470 1.2220726 1.6170006 1.3067864 1.6321536
Hartford       5.6981827  0.9612822 1.1603119 0.9417247 1.6088790 1.3932890
Houston        5.1198750  1.1917120 1.7167742 1.2353958 0.6079947 1.3586933
Indianapolis   5.5457136  0.4877551 1.3465523 0.3256257 0.7849220 0.8377371
Jacksonville   6.0881644  0.7281028 1.9378293 0.6639855 1.0036905 1.2141869
Kansas City    5.8382323  0.4916564 1.6782878 0.4846781 0.6202654 0.6483884
Little Rock    6.4008157  0.7925687 2.0934183 0.7767318 1.2278139 1.1565672
Louisville     5.7064031  0.3845377 1.4206225 0.2348055 0.9487660 0.9361662
Memphis        5.8611661  0.6065601 1.8140108 0.6196274 0.6659557 0.9939046
Miami          6.2127224  0.8486140 2.0449749 0.8726147 1.1536538 1.3892042
Milwaukee      5.4954008  0.6932461 1.4472689 0.6725777 0.6159405 0.6681029
Minneapolis    5.2134520  0.7258237 1.0696259 0.7462033 0.8748857 0.7926094
Nashville      5.9475504  0.3764923 1.7085658 0.3859196 0.9058180 0.9440643
New Orleans    6.1955606  0.7626814 2.0365725 0.7892429 1.0592654 1.2615361
Norfolk        6.0876447  0.7077797 1.6738015 0.5189483 1.2046992 1.0955637
Omaha          6.1391577  0.7127621 1.8729456 0.5855288 0.9344647 0.6542093
Philadelphia   3.0114265  2.7333218 1.7465418 2.8039681 2.5244010 2.8473451
Phoenix        6.1014483  1.2952570 2.2745414 1.2795833 1.1718477 0.7414623
Pittsburgh     5.4544838  1.0444154 0.9622567 0.9285282 1.6063427 1.3985736
Providence     5.5867947  1.9220210 1.3973202 1.8428301 2.4387303 2.2172625
Richmond       6.0446952  0.4538711 1.6810407 0.3990625 1.1149521 0.9545456
Salt Lake City 6.1891631  0.9256564 1.8491634 0.8226259 1.3251638 0.6593786
San Francisco  5.6820672  0.8554960 1.7972720 0.8634830 0.5798657 0.3824823
Seattle        5.6761047  0.4220090 1.3112539 0.3212966 1.0787634 0.9913876
St. Louis      4.9850095  1.0397959 0.6246891 1.0847478 1.3054845 1.2714482
Washington     5.4583415  0.4916817 1.2970564 0.4245087 0.7046877 0.8223218
Wichita        6.3161194  0.9850014 2.0988774 0.8725436 1.0143558 0.8329903
Wilmington     6.2222250  0.8020874 1.7265893 0.6834780 1.4439914 1.1470613
               Des Moines   Detroit  Hartford   Houston Indianapolis
Albuquerque                                                         
Atlanta                                                             
Baltimore                                                           
Buffalo                                                             
Charleston                                                          
Chicago                                                             
Cincinnati                                                          
Cleveland                                                           
Columbus                                                            
Dallas                                                              
Denver                                                              
Des Moines                                                          
Detroit         2.0984393                                           
Hartford        1.1853577 1.9421727                                 
Houston         1.5976075 1.0543383 1.8544824                       
Indianapolis    0.8436174 1.3503539 1.0219662 0.9859325             
Jacksonville    0.8451866 1.9375115 1.3167777 1.1923491    0.7386560
Kansas City     0.5749328 1.6361769 1.2121082 1.0570016    0.5023878
Little Rock     0.6033520 2.2574279 1.2510276 1.5913403    0.9782586
Louisville      0.7906048 1.5674199 0.8770470 1.1639793    0.2858565
Memphis         0.8336972 1.6793350 1.3609202 0.9153058    0.5995015
Miami           0.9700847 2.1194002 1.3955278 1.3418826    0.9826883
Milwaukee       0.9018408 1.2759027 1.3376536 0.9407396    0.5490795
Minneapolis     1.1315147 1.0657304 1.1708178 1.0893781    0.6096223
Nashville       0.6776339 1.7820568 1.0887482 1.1893820    0.5392416
New Orleans     0.8628569 2.0690650 1.3724967 1.2956543    0.9014517
Norfolk         0.5683577 1.9876912 0.8425527 1.5070888    0.6736572
Omaha           0.2278452 1.9307518 1.2484509 1.3998228    0.7191352
Philadelphia    3.3021382 1.3639247 2.7723507 2.2282108    2.5432496
Phoenix         1.1746968 2.0698843 1.8574681 1.6586312    1.3111774
Pittsburgh      1.3251163 1.6853019 0.5219248 1.7583630    0.9294636
Providence      2.0626020 2.4308259 1.0078089 2.6006293    1.8500070
Richmond        0.5896350 1.9245328 0.8822830 1.4432147    0.6332638
Salt Lake City  0.5729357 2.1051405 1.1397867 1.8144009    0.9982864
San Francisco   0.9275779 1.5234266 1.5375621 1.0922873    0.7935976
Seattle         0.8565831 1.5426220 0.8971304 1.2624972    0.4465436
St. Louis       1.4759918 1.2792352 0.7965881 1.4596166    0.9484356
Washington      0.9161919 1.2847178 1.0245078 0.9173853    0.1653802
Wichita         0.3704199 2.1249205 1.4660822 1.5040697    0.9677411
Wilmington      0.6262326 2.1829704 0.7144455 1.7985968    0.9065073
               Jacksonville Kansas City Little Rock Louisville   Memphis
Albuquerque                                                             
Atlanta                                                                 
Baltimore                                                               
Buffalo                                                                 
Charleston                                                              
Chicago                                                                 
Cincinnati                                                              
Cleveland                                                               
Columbus                                                                
Dallas                                                                  
Denver                                                                  
Des Moines                                                              
Detroit                                                                 
Hartford                                                                
Houston                                                                 
Indianapolis                                                            
Jacksonville                                                            
Kansas City       0.6389826                                             
Little Rock       0.5358469   0.6990741                                 
Louisville        0.6043504   0.5438154   0.7959221                     
Memphis           0.3770857   0.3932089   0.6856173  0.5853732          
Miami             0.3431683   0.8102372   0.5477120  0.8301535 0.5667628
Milwaukee         1.0407566   0.5207011   1.1860966  0.7672888 0.7572054
Minneapolis       1.2643090   0.8023304   1.4048482  0.8071967 1.0274482
Nashville         0.3706045   0.4158398   0.5012944  0.3744371 0.3656538
New Orleans       0.2661549   0.6794382   0.4260757  0.7533255 0.4406836
Norfolk           0.6246035   0.6874508   0.5943862  0.5009848 0.7684384
Omaha             0.7717798   0.3902246   0.6381258  0.7158075 0.6813096
Philadelphia      3.0956600   2.8609818   3.4080381  2.6996311 2.8831935
Phoenix           1.4293702   1.0915281   1.3476828  1.3414101 1.2899832
Pittsburgh        1.4036132   1.2932700   1.4832826  0.8704897 1.4321121
Providence        2.1956456   2.1372750   2.1749728  1.7565906 2.2745489
Richmond          0.5667359   0.5699330   0.4696073  0.3995623 0.6514444
Salt Lake City    1.2107358   0.8593086   0.9648167  0.9449702 1.1770594
San Francisco     1.0965402   0.5842246   1.1475603  0.9198689 0.8266978
Seattle           0.8272767   0.6815413   0.9750528  0.4146948 0.7827568
St. Louis         1.5017851   1.2283398   1.6097747  0.9837552 1.3840811
Washington        0.7694802   0.5150922   1.0125687  0.3315257 0.6029325
Wichita           0.8809829   0.5893874   0.7109279  0.9743055 0.8101452
Wilmington        0.9016490   0.8861139   0.6656869  0.7017645 1.0344450
                   Miami Milwaukee Minneapolis Nashville New Orleans   Norfolk
Albuquerque                                                                   
Atlanta                                                                       
Baltimore                                                                     
Buffalo                                                                       
Charleston                                                                    
Chicago                                                                       
Cincinnati                                                                    
Cleveland                                                                     
Columbus                                                                      
Dallas                                                                        
Denver                                                                        
Des Moines                                                                    
Detroit                                                                       
Hartford                                                                      
Houston                                                                       
Indianapolis                                                                  
Jacksonville                                                                  
Kansas City                                                                   
Little Rock                                                                   
Louisville                                                                    
Memphis                                                                       
Miami                                                                         
Milwaukee      1.2048446                                                      
Minneapolis    1.4249061 0.4292596                                            
Nashville      0.5417483 0.8213601   0.9910773                                
New Orleans    0.2087757 1.1181388   1.3558788 0.4249943                      
Norfolk        0.7689636 1.0191044   1.1332319 0.5303749   0.7257919          
Omaha          0.9319709 0.7340582   1.0066451 0.5939666   0.8075204 0.6262011
Philadelphia   3.2433342 2.5561573   2.2725670 2.9524436   3.2171123 3.0857145
Phoenix        1.6186449 1.3004609   1.4600024 1.2799300   1.4755430 1.4418698
Pittsburgh     1.5597220 1.2886656   1.0492366 1.1987700   1.5407946 0.9721762
Providence     2.2949846 2.1828234   1.9371650 2.0338805   2.2956862 1.6911379
Richmond       0.7067128 0.9633579   1.0620243 0.3102307   0.6117859 0.3687304
Salt Lake City 1.3658356 1.0839700   1.1559989 0.9607798   1.2531989 0.8466683
San Francisco  1.3035243 0.6681669   0.8775790 0.8984838   1.1605095 1.1424571
Seattle        0.9596513 0.6947741   0.6690703 0.5794792   0.9317787 0.6669291
St. Louis      1.6379488 1.1478904   0.8507733 1.2640644   1.5966229 1.2227343
Washington     1.0000736 0.5766963   0.6050620 0.5679824   0.9190833 0.7350962
Wichita        0.9939478 0.9208096   1.2438721 0.8082171   0.8836727 0.7897026
Wilmington     0.9930940 1.2150549   1.2634996 0.7245900   0.9374859 0.3732735
                   Omaha Philadelphia   Phoenix Pittsburgh Providence  Richmond
Albuquerque                                                                    
Atlanta                                                                        
Baltimore                                                                      
Buffalo                                                                        
Charleston                                                                     
Chicago                                                                        
Cincinnati                                                                     
Cleveland                                                                      
Columbus                                                                       
Dallas                                                                         
Denver                                                                         
Des Moines                                                                     
Detroit                                                                        
Hartford                                                                       
Houston                                                                        
Indianapolis                                                                   
Jacksonville                                                                   
Kansas City                                                                    
Little Rock                                                                    
Louisville                                                                     
Memphis                                                                        
Miami                                                                          
Milwaukee                                                                      
Minneapolis                                                                    
Nashville                                                                      
New Orleans                                                                    
Norfolk                                                                        
Omaha                                                                          
Philadelphia   3.1615846                                                       
Phoenix        1.0399590    3.2273305                                          
Pittsburgh     1.3450537    2.5215015 1.8667134                                
Providence     2.1401910    2.8681160 2.6063559  0.9890564                     
Richmond       0.5930259    3.0454209 1.2790155  1.0446066  1.8204081          
Salt Lake City 0.6186288    3.2408445 0.9474151  1.2334765  1.9130662 0.7741503
San Francisco  0.7204541    2.7577774 0.6784343  1.5278359  2.3649219 0.9956354
Seattle        0.8103005    2.6990165 1.5388433  0.8489738  1.7810096 0.6103324
St. Louis      1.4173965    2.0406667 1.7618172  0.7126794  1.2972807 1.2056391
Washington     0.7786054    2.4544795 1.2703379  0.9477568  1.8445969 0.6671695
Wichita        0.3133027    3.3531565 1.0953678  1.5899266  2.3240163 0.8251839
Wilmington     0.7562163    3.2354719 1.4762021  0.9596305  1.5703335 0.4351713
               Salt Lake City San Francisco   Seattle St. Louis Washington
Albuquerque                                                               
Atlanta                                                                   
Baltimore                                                                 
Buffalo                                                                   
Charleston                                                                
Chicago                                                                   
Cincinnati                                                                
Cleveland                                                                 
Columbus                                                                  
Dallas                                                                    
Denver                                                                    
Des Moines                                                                
Detroit                                                                   
Hartford                                                                  
Houston                                                                   
Indianapolis                                                              
Jacksonville                                                              
Kansas City                                                               
Little Rock                                                               
Louisville                                                                
Memphis                                                                   
Miami                                                                     
Milwaukee                                                                 
Minneapolis                                                               
Nashville                                                                 
New Orleans                                                               
Norfolk                                                                   
Omaha                                                                     
Philadelphia                                                              
Phoenix                                                                   
Pittsburgh                                                                
Providence                                                                
Richmond                                                                  
Salt Lake City                                                            
San Francisco       0.9118172                                             
Seattle             1.0290434     1.0753560                               
St. Louis           1.3714667     1.3337634 1.0288568                     
Washington          1.0279631     0.7476788 0.5382130 0.8632244           
Wichita             0.7909614     0.8558125 1.0767206 1.6368459  1.0138154
Wilmington          0.7234011     1.2757375 0.8174965 1.2659971  0.9558120
                 Wichita
Albuquerque             
Atlanta                 
Baltimore               
Buffalo                 
Charleston              
Chicago                 
Cincinnati              
Cleveland               
Columbus                
Dallas                  
Denver                  
Des Moines              
Detroit                 
Hartford                
Houston                 
Indianapolis            
Jacksonville            
Kansas City             
Little Rock             
Louisville              
Memphis                 
Miami                   
Milwaukee               
Minneapolis             
Nashville               
New Orleans             
Norfolk                 
Omaha                   
Philadelphia            
Phoenix                 
Pittsburgh              
Providence              
Richmond                
Salt Lake City          
San Francisco           
Seattle                 
St. Louis               
Washington              
Wichita                 
Wilmington     0.9338633
(distance_df_scaled <- pollution_dist
  %>% as.matrix
  %>% data.frame(check.names = FALSE) #handles the issue of replacing spaces with periods
  %>% rownames_to_column('CityA')
  %>% pivot_longer(cols = -CityA,
                   names_to = 'CityB',
                   values_to = 'Distance'
                   )
  %>% filter(CityA != CityB) #remove the distances betwen a city and itself
  
  #remove duplicate rows
  %>% rowwise()
  %>% mutate(City1 = min(CityA, CityB),
             City2 = max(CityA, CityB))
  %>% ungroup()
  %>% distinct(City1, City2, .keep_all = TRUE)
  %>% select(-c(CityA, CityB))
  %>% arrange(desc(Distance))
)
# A tibble: 820 × 3
   Distance City1       City2         
      <dbl> <chr>       <chr>         
 1     6.43 Albuquerque Chicago       
 2     6.40 Chicago     Little Rock   
 3     6.33 Charleston  Chicago       
 4     6.32 Chicago     Wichita       
 5     6.28 Chicago     Des Moines    
 6     6.22 Chicago     Wilmington    
 7     6.21 Chicago     Miami         
 8     6.20 Chicago     New Orleans   
 9     6.19 Chicago     Salt Lake City
10     6.15 Albany      Chicago       
# ℹ 810 more rows
(most_dissimilar <- distance_df_scaled[1,])
# A tibble: 1 × 3
  Distance City1       City2  
     <dbl> <chr>       <chr>  
1     6.43 Albuquerque Chicago
# Albuquerque and Chicago

(most_similar <- distance_df_scaled
  %>% arrange(desc(-Distance))
)[1,]
# A tibble: 1 × 3
  Distance City1        City2     
     <dbl> <chr>        <chr>     
1    0.165 Indianapolis Washington
# Indianapolis and Washington

Scaling accounts for the difference in variability, resulting in different answers for the most similar and dissimilar cities.

C)

Which city is Minneapolis most similar to? Most dissimilar to?

(scaled_pollution <- scale(USairpollution, center = FALSE, scale = TRUE))
                     SO2      temp       manu      popul      wind    precip
Albany         1.1971497 0.8363065 0.06002535 0.13718084 0.9102788 0.8544499
Albuquerque    0.2862749 0.9979456 0.06275377 0.28855281 0.9206229 0.1990131
Atlanta        0.6245998 1.0805220 0.50203016 0.58774895 0.9413110 1.2381327
Baltimore      1.2231747 0.9663205 0.85263275 1.07024709 0.9930314 1.0580733
Buffalo        0.2862749 0.8275218 0.53340705 0.54754078 1.2826656 0.9248857
Charleston     0.8067748 0.9698344 0.04774743 0.08396414 0.6723650 1.0437300
Chicago        2.8627493 0.8890149 4.56192625 3.98415740 1.0757840 0.8821120
Cincinnati     0.5985748 0.9487511 0.63026613 0.53571484 0.7344295 0.9999318
Cleveland      1.6916246 0.8732024 1.37376188 0.88812770 1.1275044 0.8961991
Columbus       0.6766498 0.9048274 0.36288050 0.63860047 0.8895907 0.9479374
Dallas         0.2342249 1.1630985 0.87446015 0.99810889 1.1275044 0.9205315
Denver         0.4424249 0.9118552 0.61935243 0.60903564 0.9309670 0.3316884
Des Moines     0.4424249 0.8609037 0.14187809 0.23770129 1.1585367 0.7901613
Detroit        0.9108748 0.8767163 1.45152199 1.78926392 1.0447518 0.7929787
Hartford       1.4573996 0.8626607 0.56205551 0.18684977 0.9309670 1.1108361
Houston        0.2602499 1.2105361 0.98359714 1.45813775 1.1171604 1.2342908
Indianapolis   0.7286998 0.9188830 0.49248068 0.88221473 1.0033755 0.9922479
Jacksonville   0.3643499 1.2017513 0.18553289 0.62559195 0.9102788 1.3951405
Kansas City    0.3643499 0.9575358 0.51976492 0.59957489 1.0344077 0.9476812
Little Rock    0.3383249 1.0717373 0.12414333 0.15610234 0.8482143 1.2427431
Louisville     0.7807498 0.9768622 0.39698581 0.70127793 0.8585584 1.1041767
Memphis        0.2602499 1.0822790 0.45973958 0.73793833 0.9516551 1.2575986
Miami          0.2602499 1.3264945 0.28239197 0.39616881 0.9309670 1.5316578
Milwaukee      0.4163999 0.8029245 0.77623685 0.84791952 1.2206011 0.7445701
Minneapolis    0.7547248 0.7642717 0.95358447 0.87984954 1.0964722 0.6644014
Nashville      0.4684499 1.0436262 0.37515841 0.52980187 0.8171821 1.1781983
New Orleans    0.2342249 1.1999944 0.27829933 0.42691624 0.8689025 1.4540504
Norfolk        0.8067748 1.0418692 0.13096439 0.36423879 1.0964722 1.1443891
Omaha          0.3643499 0.9048274 0.24692244 0.41035993 1.1275044 0.7730005
Philadelphia   1.7957245 0.9592927 2.30824737 2.30605727 0.9930314 1.0227274
Phoenix        0.2602499 1.2351333 0.29057724 0.68826940 0.6206446 0.1805717
Pittsburgh     1.5875246 0.8855010 0.47338170 0.61494860 0.9723433 0.9277031
Providence     2.4463494 0.8784732 0.46792485 0.21168423 1.0964722 1.0949560
Richmond       0.6766498 1.0155150 0.26874984 0.35359545 0.7861499 1.0908580
Salt Lake City 0.7286998 0.8960427 0.18689710 0.20813645 0.8999347 0.3885493
San Francisco  0.3122999 0.9961886 0.61798822 0.84673692 0.8999347 0.5291647
Seattle        0.7547248 0.8977996 0.51703650 0.62795713 0.9723433 0.9935285
St. Louis      1.4573996 0.9821330 1.05726461 0.73557314 0.9826873 0.9192508
Washington     0.7547248 1.0067303 0.59206818 0.89522326 0.9619992 0.9960898
Wichita        0.2081999 0.9944317 0.17052655 0.32757839 1.3136978 0.7832457
Wilmington     0.9368998 0.9487511 0.10913699 0.09460748 0.9309670 1.0309235
                 predays
Albany         1.1409297
Albuquerque    0.4901772
Atlanta        0.9719031
Baltimore      0.9380978
Buffalo        1.4029210
Charleston     1.2507970
Chicago        1.0310624
Cincinnati     1.1155757
Cleveland      1.3099563
Columbus       1.1324784
Dallas         0.6592038
Denver         0.7268145
Des Moines     0.8704871
Detroit        1.0902217
Hartford       1.0733191
Houston        0.8704871
Indianapolis   1.0226111
Jacksonville   0.9803544
Kansas City    0.8366818
Little Rock    0.8451331
Louisville     1.0395137
Memphis        0.8873898
Miami          1.0817704
Milwaukee      1.0395137
Minneapolis    1.1578324
Nashville      1.0057084
New Orleans    0.9550004
Norfolk        0.9803544
Omaha          0.8282305
Philadelphia   0.9719031
Phoenix        0.3042479
Pittsburgh     1.2423457
Providence     1.0564164
Richmond       0.9719031
Salt Lake City 0.7521685
San Francisco  0.5662392
Seattle        1.3860183
St. Louis      0.8873898
Washington     0.9380978
Wichita        0.6930092
Wilmington     0.9634518
attr(,"scaled:scale")
       SO2       temp       manu      popul       wind     precip    predays 
 38.424601  56.916933 733.023687 845.599122   9.667368  39.042663 118.324554 
(pollution_dist <- dist(scaled_pollution, method = 'euclidean'))
                  Albany Albuquerque   Atlanta Baltimore   Buffalo Charleston
Albuquerque    1.3160259                                                     
Atlanta        0.9811116   1.3099703                                         
Baltimore      1.2672298   1.7485984 0.8736702                               
Buffalo        1.1977179   1.3448794 0.7625596 1.2608968                     
Charleston     0.5269780   1.2913223 0.8327770 1.4123771 1.0731608           
Chicago        6.1549629   6.4291048 5.7630433 4.9990364 5.9045923  6.3292994
Cincinnati     0.9527173   1.2451397 0.3967044 0.9095928 0.7145008  0.7818271
Cleveland      1.6165369   2.2955842 1.5157617 0.8458276 1.6831014  1.8519469
Columbus       0.7924629   1.1610661 0.4101208 0.8884034 0.6510902  0.7060108
Dallas         1.6498884   1.3357696 0.8364390 1.0663987 1.0059120  1.5673516
Denver         1.2474012   0.7197309 0.9786064 1.2065654 0.9876022  1.2597542
Des Moines     0.8520111   0.7763405 0.7692941 1.3866246 0.7693095  0.7889284
Detroit        2.1848709   2.3051874 1.6405866 1.0379333 1.7173059  2.2639502
Hartford       0.6274225   1.6796022 0.9653698 0.9774218 1.3306365  0.9015397
Houston        1.9687456   1.8753176 1.0864441 1.0979563 1.2593032  1.8707182
Indianapolis   1.0049935   1.2878842 0.4366325 0.6506949 0.7382036  1.0039774
Jacksonville   1.1830189   1.3591924 0.4579465 1.2481971 0.9009028  0.9028301
Kansas City    1.1180732   1.0034137 0.4411620 1.0460278 0.6392741  0.9981672
Little Rock    1.0196058   1.1180677 0.6602323 1.4913573 0.9919576  0.6899001
Louisville     0.8367893   1.2852808 0.2973461 0.7550477 0.8081651  0.7659849
Memphis        1.2990335   1.2832298 0.4058666 1.1178364 0.7702564  1.0791481
Miami          1.3027377   1.5147526 0.6136220 1.4440504 0.9646633  0.9549577
Milwaukee      1.3219121   1.2515211 0.7677655 0.9450964 0.5789316  1.3150195
Minneapolis    1.2735798   1.4527672 0.8910573 0.7215763 0.8203734  1.3532698
Nashville      0.9793926   1.1942983 0.2556195 1.0703143 0.7374728  0.7245691
New Orleans    1.2597165   1.3794896 0.5428978 1.3960122 0.9343037  0.9198058
Norfolk        0.6290894   1.2033965 0.5052352 1.1037597 0.8796800  0.5948162
Omaha          0.9794946   0.7416626 0.6832300 1.2865138 0.7002693  0.8973179
Philadelphia   3.1930675   3.5083195 2.7659190 1.9940651 2.9694238  3.3476470
Phoenix        1.6222617   0.6273029 1.3705831 1.6648557 1.5636345  1.5610562
Pittsburgh     0.7572591   1.7551255 1.0664364 0.7748561 1.3516107  1.0878015
Providence     1.3542025   2.4504145 1.8851790 1.5540474 2.2291843  1.7634307
Richmond       0.7023559   1.1157492 0.4024271 1.0957353 0.8690474  0.4832310
Salt Lake City 0.7825922   0.5770561 1.0295976 1.3883019 1.1372413  0.8809100
San Francisco  1.4350960   0.8577580 0.9241619 1.1671717 1.0625030  1.3918986
Seattle        0.8558651   1.4097885 0.5333362 0.8588437 0.5765558  0.7967752
St. Louis      1.2309669   1.8002873 1.0695388 0.4805645 1.4356224  1.4529429
Washington     1.0706818   1.3059179 0.4303261 0.5695704 0.8365661  1.0688477
Wichita        1.1916110   0.7458033 0.8841718 1.5120235 0.8596627  1.1061675
Wilmington     0.3842754   1.1754445 0.7453606 1.2618053 1.0729548  0.4133585
                 Chicago Cincinnati Cleveland  Columbus    Dallas    Denver
Albuquerque                                                                
Atlanta                                                                    
Baltimore                                                                  
Buffalo                                                                    
Charleston                                                                 
Chicago                                                                    
Cincinnati     5.7111565                                                   
Cleveland      4.6045880  1.4423932                                        
Columbus       5.8011467  0.3423005 1.4852936                              
Dallas         5.4443167  0.9062132 1.7010357 0.9668230                    
Denver         5.7628406  0.8170377 1.7048090 0.8170683 0.8443119          
Des Moines     6.2827229  0.8021059 1.9252490 0.6574325 1.1454978 0.8069040
Detroit        4.2795496  1.5774470 1.2220726 1.6170006 1.3067864 1.6321536
Hartford       5.6981827  0.9612822 1.1603119 0.9417247 1.6088790 1.3932890
Houston        5.1198750  1.1917120 1.7167742 1.2353958 0.6079947 1.3586933
Indianapolis   5.5457136  0.4877551 1.3465523 0.3256257 0.7849220 0.8377371
Jacksonville   6.0881644  0.7281028 1.9378293 0.6639855 1.0036905 1.2141869
Kansas City    5.8382323  0.4916564 1.6782878 0.4846781 0.6202654 0.6483884
Little Rock    6.4008157  0.7925687 2.0934183 0.7767318 1.2278139 1.1565672
Louisville     5.7064031  0.3845377 1.4206225 0.2348055 0.9487660 0.9361662
Memphis        5.8611661  0.6065601 1.8140108 0.6196274 0.6659557 0.9939046
Miami          6.2127224  0.8486140 2.0449749 0.8726147 1.1536538 1.3892042
Milwaukee      5.4954008  0.6932461 1.4472689 0.6725777 0.6159405 0.6681029
Minneapolis    5.2134520  0.7258237 1.0696259 0.7462033 0.8748857 0.7926094
Nashville      5.9475504  0.3764923 1.7085658 0.3859196 0.9058180 0.9440643
New Orleans    6.1955606  0.7626814 2.0365725 0.7892429 1.0592654 1.2615361
Norfolk        6.0876447  0.7077797 1.6738015 0.5189483 1.2046992 1.0955637
Omaha          6.1391577  0.7127621 1.8729456 0.5855288 0.9344647 0.6542093
Philadelphia   3.0114265  2.7333218 1.7465418 2.8039681 2.5244010 2.8473451
Phoenix        6.1014483  1.2952570 2.2745414 1.2795833 1.1718477 0.7414623
Pittsburgh     5.4544838  1.0444154 0.9622567 0.9285282 1.6063427 1.3985736
Providence     5.5867947  1.9220210 1.3973202 1.8428301 2.4387303 2.2172625
Richmond       6.0446952  0.4538711 1.6810407 0.3990625 1.1149521 0.9545456
Salt Lake City 6.1891631  0.9256564 1.8491634 0.8226259 1.3251638 0.6593786
San Francisco  5.6820672  0.8554960 1.7972720 0.8634830 0.5798657 0.3824823
Seattle        5.6761047  0.4220090 1.3112539 0.3212966 1.0787634 0.9913876
St. Louis      4.9850095  1.0397959 0.6246891 1.0847478 1.3054845 1.2714482
Washington     5.4583415  0.4916817 1.2970564 0.4245087 0.7046877 0.8223218
Wichita        6.3161194  0.9850014 2.0988774 0.8725436 1.0143558 0.8329903
Wilmington     6.2222250  0.8020874 1.7265893 0.6834780 1.4439914 1.1470613
               Des Moines   Detroit  Hartford   Houston Indianapolis
Albuquerque                                                         
Atlanta                                                             
Baltimore                                                           
Buffalo                                                             
Charleston                                                          
Chicago                                                             
Cincinnati                                                          
Cleveland                                                           
Columbus                                                            
Dallas                                                              
Denver                                                              
Des Moines                                                          
Detroit         2.0984393                                           
Hartford        1.1853577 1.9421727                                 
Houston         1.5976075 1.0543383 1.8544824                       
Indianapolis    0.8436174 1.3503539 1.0219662 0.9859325             
Jacksonville    0.8451866 1.9375115 1.3167777 1.1923491    0.7386560
Kansas City     0.5749328 1.6361769 1.2121082 1.0570016    0.5023878
Little Rock     0.6033520 2.2574279 1.2510276 1.5913403    0.9782586
Louisville      0.7906048 1.5674199 0.8770470 1.1639793    0.2858565
Memphis         0.8336972 1.6793350 1.3609202 0.9153058    0.5995015
Miami           0.9700847 2.1194002 1.3955278 1.3418826    0.9826883
Milwaukee       0.9018408 1.2759027 1.3376536 0.9407396    0.5490795
Minneapolis     1.1315147 1.0657304 1.1708178 1.0893781    0.6096223
Nashville       0.6776339 1.7820568 1.0887482 1.1893820    0.5392416
New Orleans     0.8628569 2.0690650 1.3724967 1.2956543    0.9014517
Norfolk         0.5683577 1.9876912 0.8425527 1.5070888    0.6736572
Omaha           0.2278452 1.9307518 1.2484509 1.3998228    0.7191352
Philadelphia    3.3021382 1.3639247 2.7723507 2.2282108    2.5432496
Phoenix         1.1746968 2.0698843 1.8574681 1.6586312    1.3111774
Pittsburgh      1.3251163 1.6853019 0.5219248 1.7583630    0.9294636
Providence      2.0626020 2.4308259 1.0078089 2.6006293    1.8500070
Richmond        0.5896350 1.9245328 0.8822830 1.4432147    0.6332638
Salt Lake City  0.5729357 2.1051405 1.1397867 1.8144009    0.9982864
San Francisco   0.9275779 1.5234266 1.5375621 1.0922873    0.7935976
Seattle         0.8565831 1.5426220 0.8971304 1.2624972    0.4465436
St. Louis       1.4759918 1.2792352 0.7965881 1.4596166    0.9484356
Washington      0.9161919 1.2847178 1.0245078 0.9173853    0.1653802
Wichita         0.3704199 2.1249205 1.4660822 1.5040697    0.9677411
Wilmington      0.6262326 2.1829704 0.7144455 1.7985968    0.9065073
               Jacksonville Kansas City Little Rock Louisville   Memphis
Albuquerque                                                             
Atlanta                                                                 
Baltimore                                                               
Buffalo                                                                 
Charleston                                                              
Chicago                                                                 
Cincinnati                                                              
Cleveland                                                               
Columbus                                                                
Dallas                                                                  
Denver                                                                  
Des Moines                                                              
Detroit                                                                 
Hartford                                                                
Houston                                                                 
Indianapolis                                                            
Jacksonville                                                            
Kansas City       0.6389826                                             
Little Rock       0.5358469   0.6990741                                 
Louisville        0.6043504   0.5438154   0.7959221                     
Memphis           0.3770857   0.3932089   0.6856173  0.5853732          
Miami             0.3431683   0.8102372   0.5477120  0.8301535 0.5667628
Milwaukee         1.0407566   0.5207011   1.1860966  0.7672888 0.7572054
Minneapolis       1.2643090   0.8023304   1.4048482  0.8071967 1.0274482
Nashville         0.3706045   0.4158398   0.5012944  0.3744371 0.3656538
New Orleans       0.2661549   0.6794382   0.4260757  0.7533255 0.4406836
Norfolk           0.6246035   0.6874508   0.5943862  0.5009848 0.7684384
Omaha             0.7717798   0.3902246   0.6381258  0.7158075 0.6813096
Philadelphia      3.0956600   2.8609818   3.4080381  2.6996311 2.8831935
Phoenix           1.4293702   1.0915281   1.3476828  1.3414101 1.2899832
Pittsburgh        1.4036132   1.2932700   1.4832826  0.8704897 1.4321121
Providence        2.1956456   2.1372750   2.1749728  1.7565906 2.2745489
Richmond          0.5667359   0.5699330   0.4696073  0.3995623 0.6514444
Salt Lake City    1.2107358   0.8593086   0.9648167  0.9449702 1.1770594
San Francisco     1.0965402   0.5842246   1.1475603  0.9198689 0.8266978
Seattle           0.8272767   0.6815413   0.9750528  0.4146948 0.7827568
St. Louis         1.5017851   1.2283398   1.6097747  0.9837552 1.3840811
Washington        0.7694802   0.5150922   1.0125687  0.3315257 0.6029325
Wichita           0.8809829   0.5893874   0.7109279  0.9743055 0.8101452
Wilmington        0.9016490   0.8861139   0.6656869  0.7017645 1.0344450
                   Miami Milwaukee Minneapolis Nashville New Orleans   Norfolk
Albuquerque                                                                   
Atlanta                                                                       
Baltimore                                                                     
Buffalo                                                                       
Charleston                                                                    
Chicago                                                                       
Cincinnati                                                                    
Cleveland                                                                     
Columbus                                                                      
Dallas                                                                        
Denver                                                                        
Des Moines                                                                    
Detroit                                                                       
Hartford                                                                      
Houston                                                                       
Indianapolis                                                                  
Jacksonville                                                                  
Kansas City                                                                   
Little Rock                                                                   
Louisville                                                                    
Memphis                                                                       
Miami                                                                         
Milwaukee      1.2048446                                                      
Minneapolis    1.4249061 0.4292596                                            
Nashville      0.5417483 0.8213601   0.9910773                                
New Orleans    0.2087757 1.1181388   1.3558788 0.4249943                      
Norfolk        0.7689636 1.0191044   1.1332319 0.5303749   0.7257919          
Omaha          0.9319709 0.7340582   1.0066451 0.5939666   0.8075204 0.6262011
Philadelphia   3.2433342 2.5561573   2.2725670 2.9524436   3.2171123 3.0857145
Phoenix        1.6186449 1.3004609   1.4600024 1.2799300   1.4755430 1.4418698
Pittsburgh     1.5597220 1.2886656   1.0492366 1.1987700   1.5407946 0.9721762
Providence     2.2949846 2.1828234   1.9371650 2.0338805   2.2956862 1.6911379
Richmond       0.7067128 0.9633579   1.0620243 0.3102307   0.6117859 0.3687304
Salt Lake City 1.3658356 1.0839700   1.1559989 0.9607798   1.2531989 0.8466683
San Francisco  1.3035243 0.6681669   0.8775790 0.8984838   1.1605095 1.1424571
Seattle        0.9596513 0.6947741   0.6690703 0.5794792   0.9317787 0.6669291
St. Louis      1.6379488 1.1478904   0.8507733 1.2640644   1.5966229 1.2227343
Washington     1.0000736 0.5766963   0.6050620 0.5679824   0.9190833 0.7350962
Wichita        0.9939478 0.9208096   1.2438721 0.8082171   0.8836727 0.7897026
Wilmington     0.9930940 1.2150549   1.2634996 0.7245900   0.9374859 0.3732735
                   Omaha Philadelphia   Phoenix Pittsburgh Providence  Richmond
Albuquerque                                                                    
Atlanta                                                                        
Baltimore                                                                      
Buffalo                                                                        
Charleston                                                                     
Chicago                                                                        
Cincinnati                                                                     
Cleveland                                                                      
Columbus                                                                       
Dallas                                                                         
Denver                                                                         
Des Moines                                                                     
Detroit                                                                        
Hartford                                                                       
Houston                                                                        
Indianapolis                                                                   
Jacksonville                                                                   
Kansas City                                                                    
Little Rock                                                                    
Louisville                                                                     
Memphis                                                                        
Miami                                                                          
Milwaukee                                                                      
Minneapolis                                                                    
Nashville                                                                      
New Orleans                                                                    
Norfolk                                                                        
Omaha                                                                          
Philadelphia   3.1615846                                                       
Phoenix        1.0399590    3.2273305                                          
Pittsburgh     1.3450537    2.5215015 1.8667134                                
Providence     2.1401910    2.8681160 2.6063559  0.9890564                     
Richmond       0.5930259    3.0454209 1.2790155  1.0446066  1.8204081          
Salt Lake City 0.6186288    3.2408445 0.9474151  1.2334765  1.9130662 0.7741503
San Francisco  0.7204541    2.7577774 0.6784343  1.5278359  2.3649219 0.9956354
Seattle        0.8103005    2.6990165 1.5388433  0.8489738  1.7810096 0.6103324
St. Louis      1.4173965    2.0406667 1.7618172  0.7126794  1.2972807 1.2056391
Washington     0.7786054    2.4544795 1.2703379  0.9477568  1.8445969 0.6671695
Wichita        0.3133027    3.3531565 1.0953678  1.5899266  2.3240163 0.8251839
Wilmington     0.7562163    3.2354719 1.4762021  0.9596305  1.5703335 0.4351713
               Salt Lake City San Francisco   Seattle St. Louis Washington
Albuquerque                                                               
Atlanta                                                                   
Baltimore                                                                 
Buffalo                                                                   
Charleston                                                                
Chicago                                                                   
Cincinnati                                                                
Cleveland                                                                 
Columbus                                                                  
Dallas                                                                    
Denver                                                                    
Des Moines                                                                
Detroit                                                                   
Hartford                                                                  
Houston                                                                   
Indianapolis                                                              
Jacksonville                                                              
Kansas City                                                               
Little Rock                                                               
Louisville                                                                
Memphis                                                                   
Miami                                                                     
Milwaukee                                                                 
Minneapolis                                                               
Nashville                                                                 
New Orleans                                                               
Norfolk                                                                   
Omaha                                                                     
Philadelphia                                                              
Phoenix                                                                   
Pittsburgh                                                                
Providence                                                                
Richmond                                                                  
Salt Lake City                                                            
San Francisco       0.9118172                                             
Seattle             1.0290434     1.0753560                               
St. Louis           1.3714667     1.3337634 1.0288568                     
Washington          1.0279631     0.7476788 0.5382130 0.8632244           
Wichita             0.7909614     0.8558125 1.0767206 1.6368459  1.0138154
Wilmington          0.7234011     1.2757375 0.8174965 1.2659971  0.9558120
                 Wichita
Albuquerque             
Atlanta                 
Baltimore               
Buffalo                 
Charleston              
Chicago                 
Cincinnati              
Cleveland               
Columbus                
Dallas                  
Denver                  
Des Moines              
Detroit                 
Hartford                
Houston                 
Indianapolis            
Jacksonville            
Kansas City             
Little Rock             
Louisville              
Memphis                 
Miami                   
Milwaukee               
Minneapolis             
Nashville               
New Orleans             
Norfolk                 
Omaha                   
Philadelphia            
Phoenix                 
Pittsburgh              
Providence              
Richmond                
Salt Lake City          
San Francisco           
Seattle                 
St. Louis               
Washington              
Wichita                 
Wilmington     0.9338633
(distance_df_mn <- pollution_dist
  %>% as.matrix
  %>% data.frame(check.names = FALSE) #handles the issue of replacing spaces with periods
  %>% rownames_to_column('CityA')
  %>% pivot_longer(cols = -CityA,
                   names_to = 'CityB',
                   values_to = 'Distance'
                   )
  %>% filter(CityA == 'Minneapolis' & CityA != CityB) #filters to Minneapolis and removes duplicate entries
  %>% arrange(desc(Distance))
)
# A tibble: 40 × 3
   CityA       CityB        Distance
   <chr>       <chr>           <dbl>
 1 Minneapolis Chicago          5.21
 2 Minneapolis Philadelphia     2.27
 3 Minneapolis Providence       1.94
 4 Minneapolis Phoenix          1.46
 5 Minneapolis Albuquerque      1.45
 6 Minneapolis Miami            1.42
 7 Minneapolis Little Rock      1.40
 8 Minneapolis New Orleans      1.36
 9 Minneapolis Charleston       1.35
10 Minneapolis Albany           1.27
# ℹ 30 more rows
(most_dissimilar <- distance_df_mn[1,])
# A tibble: 1 × 3
  CityA       CityB   Distance
  <chr>       <chr>      <dbl>
1 Minneapolis Chicago     5.21
# Minneapolis is most dissimilar to Chicago

(most_similar <- distance_df_mn
  %>% arrange(desc(-Distance))
)[1,]
# A tibble: 1 × 3
  CityA       CityB     Distance
  <chr>       <chr>        <dbl>
1 Minneapolis Milwaukee    0.429
# Minneapolis is most similar to Milwaukee