คำนวณและพล็อตระยะห่างระหว่างผู้เล่นสองคน
คุณได้รับพิกัดของผู้เล่นสองคนในแมตช์ฟุตบอล โดยวัดจากจุดกึ่งกลางสนาม และต้องการคำนวณระยะห่างระหว่างผู้เล่นทั้งสอง
ในแบบฝึกหัดนี้ จะพล็อตตำแหน่งของผู้เล่นทั้ง 2 คน และคำนวณระยะห่างด้วยตนเองโดยใช้สูตร Euclidean distance
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
การวิเคราะห์กลุ่มข้อมูลใน R
คำแนะนำการฝึกหัด
- พล็อตตำแหน่งของผู้เล่นจาก data frame
two_playersโดยใช้ggplot - ดึงตำแหน่งของผู้เล่นแต่ละคนออกมาเป็น data frame ชื่อ
player1และplayer2 - คำนวณระยะห่างระหว่าง player1 และ player2 โดยใช้สูตร Euclidean distance $$\sqrt{(x_1-x_2)^2+(y_1-y_2)^2}$$
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
# 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