Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 8th, 2012  |  syntax: None  |  size: 4.21 KB  |  hits: 7  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Downloading Live Olympic Medal Data into R
  2. library(XML)
  3. library(RCurl)
  4. theurl <- "http://www.london2012.com/medals/medal-count/"
  5. page <- getURL(theurl)
  6.  
  7. page # fail
  8. [1] "<HTML><HEAD>n<TITLE>Access Denied</TITLE>n</HEAD><BODY>n<H1>Access Denied</H1>n nYou don't have permission to access "http&#58;&#47;&#47;www&#46;london2012&#46;com&#47;medals&#47;medal&#45;count&#47;" on this server.<P>nReference&#32;&#35;18&#46;358a503f&#46;1343590091&#46;c056ae2n</BODY>n</HTML>n"
  9.        
  10. page <- readHTMLTable(theurl)
  11.        
  12. page <- getURLContent(theurl, useragent="Mozilla/5.0 (Windows NT 6.1; rv:15.0) Gecko/20120716 Firefox/15.0a2")
  13.        
  14. rr <- readHTMLTable(page,header=FALSE)
  15. rr2 <- setNames(rr[[1]],
  16.                 c("rank","country","gold","silver","bronze","junk","total"))
  17. rr3 <- subset(rr2,select=-junk)
  18. ## oops, numbers all got turned into factors ...
  19. tmpf <- function(x) { as.numeric(as.character(x)) }
  20. rr3[,-2] <- sapply(rr3[,-2],tmpf)              
  21. head(rr3)
  22. ##   rank                                country gold silver bronze total
  23. ## 1    1             People's Republic of China    6      4      2    12
  24. ## 2    2               United States of America    3      5      3    11
  25. ## 3    3                                  Italy    2      3      2     7
  26. ## 4    4                      Republic of Korea    2      1      2     5
  27. ## 5    5                                 France    2      1      1     4
  28. ## 6    6 Democratic People's Republic  of Korea    2      0      1     3
  29. with(rr3,dotchart(total,country))
  30.        
  31. # file <- "~/Documents/R/medals.html"
  32. # page <- readChar(file,file.info(file)$size)
  33.  
  34. library(RCurl)
  35. theurl <- "http://www.london2012.com/medals/medal-count/"
  36. page <- getURLContent(theurl, useragent="Mozilla/5.0 (Windows NT 6.1; rv:15.0) Gecko/20120716 Firefox/15.0a2")
  37.  
  38.  
  39. # Remove html tags:
  40. page <- gsub("<(.|n)*?>","",page)
  41. # Remove newlines and tabs:
  42. page <- gsub("\n","",page)
  43.  
  44. # match table:
  45. page <- regmatches(page,regexpr("(?<=Total).*(?=Detailed)",page,perl=TRUE))
  46.  
  47. # Extract country+medals+rank
  48. codes <-regmatches(page,gregexpr("\d+[^\r]*\d+",page,perl=TRUE))[[1]]
  49. codes <- codes[seq(1,length(codes)-2,by=2)]
  50.  
  51. # Extract country and medals:
  52. Names <- gsub("\d","",codes)
  53. Medals <- sapply(regmatches(codes,gregexpr("\d",codes)),function(x)x[(length(x)-2):length(x)])
  54.  
  55. # Create data frame:
  56. data.frame(
  57.   Country = Names,
  58.   Gold = as.numeric(Medals[1,]),
  59.   Silver = as.numeric(Medals[2,]),
  60.   Bronze = as.numeric(Medals[3,]))
  61.        
  62. Country Gold Silver Bronze
  63. 1              People's Republic of China    6      4      2
  64. 2                United States of America    3      5      3
  65. 3                                   Italy    2      3      2
  66. 4                       Republic of Korea    2      1      2
  67. 5                                  France    2      1      1
  68. 6  Democratic People's Republic  of Korea    2      0      1
  69. 7                              Kazakhstan    2      0      0
  70. 8                               Australia    1      1      1
  71. 9                                  Brazil    1      1      1
  72. 10                                Hungary    1      1      1
  73. 11                            Netherlands    1      1      0
  74. 12                     Russian Federation    1      0      3
  75. 13                                Georgia    1      0      0
  76. 14                           South Africa    1      0      0
  77. 15                                  Japan    0      2      3
  78. 16                          Great Britain    0      1      1
  79. 17                               Colombia    0      1      0
  80. 18                                   Cuba    0      1      0
  81. 19                                 Poland    0      1      0
  82. 20                                Romania    0      1      0
  83. 21                Taipei (Chinese Taipei)    0      1      0
  84. 22                             Azerbaijan    0      0      1
  85. 23                                Belgium    0      0      1
  86. 24                                 Canada    0      0      1
  87. 25                    Republic of Moldova    0      0      1
  88. 26                                 Norway    0      0      1
  89. 27                                 Serbia    0      0      1
  90. 28                               Slovakia    0      0      1
  91. 29                                Ukraine    0      0      1
  92. 30                             Uzbekistan    0      0      1