计算 r 中组间的欧式距离
Calculate euclidean distance between groups in r
有一个包含 3 个时刻 (36,76,96) 的匹配跟踪数据集。
我的要求是计算给定玩家和对手之间的距离。
Dataframe 包含以下 5 列
- time_id (second or instant)
- player ( identifier for player)
- x (x position)
- y (y position)
- team (home or away)
以主场球员为例 = 26
我需要用
计算距离
所有客场球员(“12”,“17”,“24”,“37”,“69”,“77”)
3 个不同的 time_id (36,76,96)
这里可以看到df数据
https://pasteboard.co/ICiyyFB.png
这里是link用df下载示例rds
https://1drv.ms/u/s!Am7buNMZi-gwgeBpEyU0Fl9ucem-bw?e=oSTMhx
library(tidyverse)
dat <- readRDS(file = "dat.rds")
# Given home player with id 26
# I need to calculate on each time_id the euclidean distance
# with all away players on each time_id
p36_home <- dat %>% filter(player ==26)
# all away players
all_away <- dat %>% filter(team =='away')
# I know I can calculate it if i put on columns but not elegant
# and require it group by time_id
# mutate(dist= round( sqrt((x1-x2)^2 +(y1-y2)^2),2) )
# below distances row by row should be calculated
# time_id , homePlayer, awayPlayer , distance
#
# 36 , 26 , 12 , x
# 36 , 26 , 17 , x
# 36 , 26 , 24 , x
# 36 , 26 , 37 , x
# 36 , 26 , 69 , x
# 36 , 26 , 77 , x
#
# 76 , 26 , 12 , x
# 76 , 26 , 17 , x
# 76 , 26 , 24 , x
# 76 , 26 , 37 , x
# 76 , 26 , 69 , x
# 76 , 26 , 77 , x
#
# 96 , 26 , 12 , x
# 96 , 26 , 17 , x
# 96 , 26 , 24 , x
# 96 , 26 , 37 , x
# 96 , 26 , 69 , x
# 96 , 26 , 77 , x
此解决方案应该适合您。我只是加入了您提供的两个数据框并使用了您的距离计算。然后过滤列以获得所需的结果。
test <- left_join(p36_home,all_away,by="time_id")
test$dist <- round( sqrt((test$x.x-test$x.y)^2 +(test$y.x-test$y.y)^2),2)
test <- test[,c(1,2,6,10)]
names(test) <- c("time_id",'homePlayer','awayPlayer','distance')
test
结果看起来像这样:
time_id homePlayer awayPlayer distance
36 26 37 26.43
36 26 17 28.55
36 26 24 20.44
36 26 69 24.92
36 26 77 11.22
36 26 12 22.65
.
.
.
有一个包含 3 个时刻 (36,76,96) 的匹配跟踪数据集。 我的要求是计算给定玩家和对手之间的距离。
Dataframe 包含以下 5 列
- time_id (second or instant)
- player ( identifier for player)
- x (x position)
- y (y position)
- team (home or away)
以主场球员为例 = 26
我需要用
计算距离所有客场球员(“12”,“17”,“24”,“37”,“69”,“77”)
3 个不同的 time_id (36,76,96)
这里可以看到df数据 https://pasteboard.co/ICiyyFB.png
这里是link用df下载示例rds https://1drv.ms/u/s!Am7buNMZi-gwgeBpEyU0Fl9ucem-bw?e=oSTMhx
library(tidyverse)
dat <- readRDS(file = "dat.rds")
# Given home player with id 26
# I need to calculate on each time_id the euclidean distance
# with all away players on each time_id
p36_home <- dat %>% filter(player ==26)
# all away players
all_away <- dat %>% filter(team =='away')
# I know I can calculate it if i put on columns but not elegant
# and require it group by time_id
# mutate(dist= round( sqrt((x1-x2)^2 +(y1-y2)^2),2) )
# below distances row by row should be calculated
# time_id , homePlayer, awayPlayer , distance
#
# 36 , 26 , 12 , x
# 36 , 26 , 17 , x
# 36 , 26 , 24 , x
# 36 , 26 , 37 , x
# 36 , 26 , 69 , x
# 36 , 26 , 77 , x
#
# 76 , 26 , 12 , x
# 76 , 26 , 17 , x
# 76 , 26 , 24 , x
# 76 , 26 , 37 , x
# 76 , 26 , 69 , x
# 76 , 26 , 77 , x
#
# 96 , 26 , 12 , x
# 96 , 26 , 17 , x
# 96 , 26 , 24 , x
# 96 , 26 , 37 , x
# 96 , 26 , 69 , x
# 96 , 26 , 77 , x
此解决方案应该适合您。我只是加入了您提供的两个数据框并使用了您的距离计算。然后过滤列以获得所需的结果。
test <- left_join(p36_home,all_away,by="time_id")
test$dist <- round( sqrt((test$x.x-test$x.y)^2 +(test$y.x-test$y.y)^2),2)
test <- test[,c(1,2,6,10)]
names(test) <- c("time_id",'homePlayer','awayPlayer','distance')
test
结果看起来像这样:
time_id homePlayer awayPlayer distance
36 26 37 26.43
36 26 17 28.55
36 26 24 20.44
36 26 69 24.92
36 26 77 11.22
36 26 12 22.65
.
.
.