始める無料で始める

2人の選手間の距離を計算してプロットする

サッカーの試合において、フィールドの中心を基準とした2人の選手の座標を取得しました。次は、この2人の選手間の距離を計算してみましょう。

この演習では、2人の選手の位置をプロットし、ユークリッド距離の公式を使って手動で距離を計算します。

この演習はコースの一部です

Rによるクラスター分析

コースを見る

演習の手順

  • two_players データフレームから ggplot を使って、選手の位置をプロットしましょう。
  • 各選手の位置を player1player2 という2つのデータフレームに取り出しましょう。
  • ユークリッド距離の公式 $$\sqrt{(x_1-x_2)^2+(y_1-y_2)^2}$$ を使って、player1 と player2 の距離を計算しましょう。

実践的なインタラクティブ演習

このサンプルコードを完成させて、この演習に挑戦してみましょう。

# Plot the positions of the players
ggplot(___, aes(x = ___, y = ___)) + 
  geom_point() +
  # Assuming a 40x60 field
  lims(x = c(-30,30), y = c(-20, 20))

# Split the players data frame into two observations
player1 <- two_players[___, ]
player2 <- two_players[___, ]

# Calculate and print their distance using the Euclidean Distance formula
player_distance <- sqrt( (player1$___ - player2$___)^2 + (player1$___ - player2$___)^2 )
player_distance
コードを編集して実行