开始使用免费开始使用

计算并绘制两名球员之间的距离

您已经获得了一场足球比赛中两名球员相对于球场中心的坐标,想要计算他们之间的距离。

在本练习中,您将先用图展示这两名球员的位置,然后使用欧氏距离公式手动计算他们之间的距离。

本练习是课程的一部分

R 中的聚类分析

查看课程

练习说明

  • 使用 ggplot 从数据框 two_players 中绘制两名球员的位置。
  • 将两名球员的位置分别提取到两个数据框 player1player2 中。
  • 使用欧氏距离公式 $$\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
编辑并运行代码