Amc
Amc

Reputation: 159

Distance between clusters and observations with k-means R

Data and code

mtcars

k<-kmeans(mtcars,4)

I want to create 4 columns in the df mtcars that describe the euclidian distance of each observation (row) to each centroid defined in the object k.

Upvotes: 1

Views: 70

Answers (1)

Rui Barradas
Rui Barradas

Reputation: 76402

Create a function to compute the Euclidian distances and apply it to the data set in loop by rows. The only point to be careful about is that R's tabular data is in column major order so the centers matrix must be transposed in order for the subtraction to work properly.

euclidean <- function(x, centers) sqrt(colSums((t(centers) - x)^2))

k <- kmeans(mtcars, 4)
t(apply(as.matrix(mtcars), 1, euclidean, k$centers))
#>                             1         2          3         4
#> Mazda RX4           133.72129  65.46177  12.345798 258.92953
#> Mazda RX4 Wag       133.71531  65.44662  12.259517 258.92886
#> Datsun 710          188.41664  13.94168  64.701099 313.01491
#> Hornet 4 Drive       55.41632 159.15038  92.052088 178.71490
#> Hornet Sportabout    77.28071 275.02928 201.090012  63.87335
#> Valiant              80.03499 126.03021  60.487497 206.97900
#> Duster 360          114.95384 306.18569 230.325991  31.04302
#> Merc 240D           168.42041  49.23091  61.672122 295.63710
#> Merc 230            157.34727  41.96413  36.468185 283.10534
#> Merc 280            122.24402  78.51645   3.254434 246.19090
#> Merc 280C           122.22901  78.66753   3.845208 246.17432
#> Merc 450SE           23.39669 200.54618 124.642887 123.93437
#> Merc 450SL           23.39104 200.49626 124.616832 123.95287
#> Merc 450SLC          23.44417 200.60781 124.677153 123.93000
#> Cadillac Fleetwood  193.27709 390.90757 317.160470  88.18066
#> Lincoln Continental 184.48715 382.93031 308.548984  73.93793
#> Chrysler Imperial   171.19059 369.87388 294.741443  51.84679
#> Fiat 128            226.21403  28.10039 104.019421 351.78526
#> Honda Civic         234.82029  39.32253 114.105502 361.10079
#> Toyota Corolla      233.66102  35.29959 111.194950 359.03860
#> Toyota Corona       175.65754  24.92816  52.070232 300.38940
#> Dodge Challenger     34.49859 227.39238 154.449452 108.07101
#> AMC Javelin          21.21994 214.12084 140.753501 117.65007
#> Camaro Z28          108.69281 297.84299 222.027705  40.37769
#> Pontiac Firebird    116.65783 312.90351 239.814335  58.53188
#> Fiat X1-9           225.64529  27.33179 103.276775 351.29772
#> Porsche 914-2       177.83482  21.22157  55.124411 303.07093
#> Lotus Europa        195.35043  32.50827  72.666862 316.84034
#> Ford Pantera L      124.90042 309.39889 233.985129  49.11348
#> Ferrari Dino        140.68518 103.62545  59.087014 249.91627
#> Maserati Bora       177.57273 322.97041 253.575991 134.96902
#> Volvo 142E          170.96053  34.23275  47.002633 294.34492

Created on 2023-02-07 with reprex v2.0.2

Upvotes: 1

Related Questions