시작하기무료로 시작하기

두 선수 간의 거리 계산 및 시각화

축구 경기에서 두 선수의 필드 중심 기준 좌표를 얻었고, 이들 사이의 거리를 계산하려고 합니다.

이 연습 문제에서는 두 선수의 위치를 시각화하고, 유클리드 거리 공식을 사용하여 두 선수 사이의 거리를 직접 계산해 보겠습니다.

이 연습은 강의의 일부입니다

R로 배우는 군집 분석

강의 보기

연습 안내

  • two_players 데이터 프레임을 ggplot으로 그려 두 선수의 위치를 시각화하세요.
  • 두 선수의 위치를 각각 player1, player2라는 두 개의 데이터 프레임으로 추출하세요.
  • 유클리드 거리 공식 $$\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
코드 편집 및 실행