開始使用免費開始

使用 dist() 函式

手動套用歐氏距離公式在 2 個觀測值時還算可行,但當你要比較很多觀測值時,很快就會變得複雜。

dist() 函式能簡化這個流程,利用特徵(欄)來計算觀測值(列)之間的距離。在這個例子裡,觀測值是球員的位置,維度是他們的 x 與 y 座標。

注意:dist() 的預設距離計算是歐氏距離(Euclidean distance)

本練習屬於課程

R 的叢集分析

檢視課程

練習說明

  • 使用 dist() 函式,計算資料框 two_players 中兩位球員之間的距離。
  • 計算資料框 three_players 中三位球員之間的距離。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Calculate the Distance Between two_players
dist_two_players <- ___
dist_two_players

# Calculate the Distance Between three_players
dist_three_players <- ___
dist_three_players
編輯並執行程式碼