I do a network analysis on football passing network and have a dataset with passes and substitutions. My goal is to compute a 50-pass network before a substitution and consequently compute the density of all these networks (so a single density metric per substitution). In total the dataset includes 307 substitutions, so I want to have 307 density values.
pass.sub_dataframe
Is the overall dataset, which includes for a pass two player names (one sender and one receiver). For substitutions it also includes two player names (one leaving the field and one entering the field)
The overall dataset includes the following variables: 'match_id', which is a unique identifier for every match;'type.name', which can be ''Substitution'' or ''Pass'' 'player.name', which is either the sender ('Pass') or player leaving the field ('Substitution'); 'pass.recipient.name' which is the player receiving the ball ('Pass'); 'substitution.replacement.name' which is the player entering the field ('Substitution'
With a For loop I first try to compute the passing network per match; then create the 50-passes network for the substitutions in that match; then create the graph and calculate the density Code:
match_ids <- names(table(pass.sub_dataframe$match_id))
network_metrics <- numeric(length(match_ids))
Create For Loop
for(i in 1:length(match_ids)){
# get passes and substitutions for single match ID
match_i <- pass.sub_dataframe[pass.sub_dataframe$match_id == match_ids[i], ]
# get passes and substitutions for match ID
match_i <- pass.sub_dataframe[pass.sub_dataframe$match_id == match_ids[i], ]
# create 50-passes window before substitution
network_before <- match_i[(((which(match_i$type.name == "Substitution", arr.ind=TRUE))-50):(which(match_i$type.name == "Substitution", arr.ind=TRUE)) -1), ]
network_before <- network_before[, c("player.name", "pass.recipient.name")]
# Create graph
gPassBefore <- graph_from_data_frame(network_before)
# Density
density <- igraph::ecount(gPassBefore)/(vcount(gPassBefore)*(vcount(gPassBefore)-1))
# Store values
network_metrics <- density
}
Again, I get a single output, which is the density of the 50-pass network of last match (last unique match_id). Besides that several substitution still should be accounted for, what goes wrong with the for loop? Can it have anything to do with the lack of indexing after the 'get passes and substitutions for single match ID' step?
To overcome this I tried to find an alternative to create the 50-pass network before substitution with another for loop but this attempt was unsuccesful
Hope someone can help!