How to: Import NFL Data into R / R Studio – HTML import


If you want to start running some cool analysis on NFL data (or any other sport for that matter) there are a number of ways to do it. This mini-tutorial will show you how to import data for punters in the NFL for 2014. You should be able to follow this same model for any position or general offense or defense:

First, get the year. We want to add the year here just in case you want to grab punter data for ten years rather than import it through the url:

>year <- 2014 Then, here is where the magic happens. Below add the url and you will prep R to grab the data from this URL. Note position is punter and year is pulling from the year attribute above: >url <- paste("",year,"&timeframe=All&qualified=0&sort=411&old_category=P") Next, you’ll assign the above url to punter and read the HTML table data: >punter <- readHTMLTable(url, encoding = "UTF-8", colClasses="character")[[7]] There are two empty columns at 4 and 14, so we will remove those and create a new data frame: <- punter[,-c(4,14)] Now we need to name our headers: >names( <- c("Name","Team","G","Punt","Yds","Avg","Long","In20","In10","FC","TB","Blk") Remove the headers that were imported into the first row: > =[-1,]

And remove the first column:
>row.names( <- NULL Now, take a look at your data frame for and you should see all of your data ordered and available to start running some data analysis.


Please enter your comment!
Please enter your name here