# Fantasy Football Player Forecasting in R

I rewrote my previous post in R, mainly because Paul Rubin mentioned the fact that SAS costs too much on Twitter:

The R code is a bit shorter (120 lines instead of 157), but they are basically the same complexity. I am following exactly the same logic as last time so I won’t go into the football part of it. I will simply highlight a couple of things about the code.

I rely on read.csv to read each input file into a data frame. Once I have a data frame, the primary operation is to “score” the position. Here is the code to score the running back position. The “within” statement makes it easy.

```score.rb <- function(rb) {
rb <- within(rb, {
FFPts <- (Rush.TD + Rec.TD) * PtsTD + FumL * PtsFum + Rush.Yds / RushYdsPt + Rec.Yds / RecYdsPt
})
return(rb)
}```

Remember from the last post that the next thing I do is to “normalize” the scores by subtracting the score of the projected worst starter on each team. This means sorting the players by score and picking one in a particular slot. Here’s how that works:

```normalize <- function(p, count) {
FFPtsMin <- as.numeric(p[with(p, order(-as.numeric(FFPts))), ][count,"FFPts"])
p\$FFPtsN <- apply(p,1,function(row) (as.numeric(row["FFPts"]) - FFPtsMin))
return(p)
}```

So by calling score.rb and normalize in sequence, I get final scores for the RB position. Once I score all of the positions, I simply need to join the data frames for each position together into a final data frame (called “players”). I write this out to CSV. I have saved the complete rankings so you can see the results. 