Overview
A simple example of basic calculations on football data is to calculate the total passing yards for a quarterback or group of quarterbacks.
Implementation
Passing yards is a column of the player stats dataset. It can be loaded and exported from R as follows:
pstats <- nflreadr::load_player_stats(2021)
write.csv(dataset, "table.csv")
Next, the techniques described in Filtering and Transforming Data can be used to calculate total passing yards. In particular, the following code uses the $list api to filter on Mahomes records, select the passing_yards column and to sum.
let totalPassing = $list(data)
.filter(p=>p.player_name==='P.Mahomes')
.map(p=>p.passing_yards)
.sum();
Grouping
Grouping
let group = $group(data, p=>p.player_name).toArray().map(p=>{
return {
player:p.keys[0],
total:$list(p.values).map(p=>p.passing_yards).sum()
}
});
$val.set('totals', $list(group).filter(p=>p.total>0).sort(p=>p.total).reverse().items);