As the doctor gone rogue

June 7, 2010

Import HTML table to R

Filed under: R — hypotheses @ 11:29 pm

Ever remember the day you have to copy a table from WWW paste it into excel and still have to spend more time fixing it into the format you want, and still can’t do nothing else to it beside looking at an Excel table?
During the past couple days, I have been looking around and saw that they have already incorporate this functionality in R. I also read on R-blogger today http://bit.ly/9UawIE The process to import the table seems pretty simple. Here’s how.
library(XML)
# URL for the Google Data
u=”http://www.google.com/adplanner/static/top1000/”
tables = readHTMLTable(u)
my.table=tables[[2]] # The first element of the list is empty

Compare to the other method which import the table to a list. Then you will need to convert those list into the appropriate table. http://r.789695.n4.nabble.com/Read-HTML-table-td840241.html

xpathApply( htmlTreeParse("http://blabla", useInt=T), "//td", function(x) xmlValue(x))

Advertisements

Blog at WordPress.com.