An interesting and useful question, thanks Steve.
I also thank jschmidt 17654 for sharing a perfect general solution, well done. 🙂
# Create the data frame.
rank <- c(1,2,3,4,5)
player<- c("Tom Brady","Philip Rivers", "Matthew Stafford", "Drew Brees", "Ben Roethlisberger")
year <- 2017
yards <- c(4577, 4515, 4446, 4334, 4251)
passing.2017<-data.frame(rank, player, year, yards)
colnames(passing.2017) <- c("rank", "player.name", "year2017", "yards2017")
print(passing.2017)# Add a new column trailingyards with yards differences.
passing.2017$trailingyards <- passing.2017[which.max(passing.2017$yards), "yards2017"] - passing.2017$yards2017
print(passing.2017)Results:
Executing the program....
$Rscript main.rrank player.name year2017 yards2017
1 1 Tom Brady 2017 4577
2 2 Philip Rivers 2017 4515
3 3 Matthew Stafford 2017 4446
4 4 Drew Brees 2017 4334
5 5 Ben Roethlisberger 2017 4251
rank player.name year2017 yards2017 trailingyards
1 1 Tom Brady 2017 4577 0
2 2 Philip Rivers 2017 4515 62
3 3 Matthew Stafford 2017 4446 131
4 4 Drew Brees 2017 4334 243
5 5 Ben Roethlisberger 2017 4251 326