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

0

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("http://sports.yahoo.com/nfl/stats/byposition?pos=P&conference=NFL&year=season_",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.final <- punter[,-c(4,14)] Now we need to name our headers: >names(punter.final) <- c("Name","Team","G","Punt","Yds","Avg","Long","In20","In10","FC","TB","Blk") Remove the headers that were imported into the first row: >punter.final = punter.final[-1,]

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

LEAVE A REPLY

Please enter your comment!
Please enter your name here