【发布时间】:2019-10-16 22:33:33
【问题描述】:
有一个包含 3 个时间点 (36,76,96) 的跟踪数据集进行匹配。 我的要求是计算给定玩家和对手之间的距离。
数据框包含以下 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
这里是使用 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
【问题讨论】:
-
大卫,请考虑两件事:(1) 添加实际示例数据(文本),而不是图像:图像会破坏屏幕阅读器,无法搜索,在移动设备上并不总是理想地显示,并且不允许轻松复制/粘贴到本地会话进行测试; (2) 当样本数据的链接失效时,这个问题变得无法重现,请通过提供样本数据提供一个最小的工作示例(参考:stackoverflow.com/questions/5963269)。谢谢!
标签: r euclidean-distance