0

Having a tracking dataset with 3 time moments (36,76,96) for a match. My requirement is to calculate distances between a given player and opponents.

Dataframe contains following 5 columns

- time_id (second or instant)

- player ( identifier for player)

- x (x position)

- y (y position)

- team (home or away)

As an example for home player = 26

I need to calculate distances with

all away players ( "12","17","24","37","69","77" ) in the

3 distinct time_id (36,76,96)

Here we can see df data https://pasteboard.co/ICiyyFB.png

Here it is the link to download sample rds with df 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

  • David, please consider two things: (1) add actual sample data (text), not an image of it: images break screen readers, cannot be searched, are not always displayed ideally on mobile devices, and do not allow easy copy/paste to local sessions for testing; and (2) when the link to sample data goes stale, this question becomes unreproducible, please provide a minimal working example by giving sample data (ref: https://stackoverflow.com/questions/5963269). Thanks! – r2evans Oct 16 '19 at 22:53

1 Answers1

1

This solution should work for you. I simply joined the two dataframes you provided and used your distance calculation. Then filtered the columns to get the desired result.

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

The result looks something like this:

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
.
.
.
ErrorJordan
  • 611
  • 5
  • 15
  • Thanks @errorjordan , this solves my initial problem, now I need to filter time_ids without opponent players in a distance of 10 metres or less. – David Fombella Pombal Oct 17 '19 at 14:30
  • @DavidFombellaPombal I'm not sure I fully understand your next step. If you elaborate (add on to your current question or create a new one), I'll try to help you out. – ErrorJordan Oct 17 '19 at 14:33