2012-04-08 117 views
4

我正在學習用「R中數據混搭」 R,我不能讓頁上的例子。 5工作。我使用的代碼是這樣的:數據混搭:「下標越界」

# Install and load RCurl 
install.packages("RCurl") 
library("RCurl") 

# Install and load the XML package 
install.packages("XML") 
library("XML") 

# Download file and parse it 
appid<- 'ucVVQzLV34GQR4ppLwbdW6G8cCSZDoCBqAc53NXsWB3gXkmP1I4epLwMxboV.PfADi_2ubr2A7Cg8FO4Z3xVxxujza2FJ 8M-' 
street<-"11408 Bellflower Road" 
RCurl<-paste( 
"http://local.yahooapis.com/MapsService/V1/geocode?appid=", 
appid, 
"&street=", 
URLencode(street), 
"&city=Cleveland&state=OH" 
,sep="") 
#xmlResult<-xmlTreeParse(requestUrl,isURL=TRUE) 
xmlResult<-xmlTreeParse(getURL(RCurl)) 

#Print the output 
str(xmlResult) 

但我做到這一點時,我沒有得到以下結果:

List of 2 
$ doc:List of 3 
    ..$ file :List of 2 
    .. ..$ text : Named list() 
    .. .. ..- attr(*, "class")= chr [1:5] "XMLTextNode" "XMLNode" "RXMLAbstractNode"  "XMLAbstractNode" ... 
    .. ..$ Message:List of 1 
    .. .. ..$ text: Named list() 
    .. .. .. ..- attr(*, "class")= chr [1:5] "XMLTextNode" "XMLNode" "RXMLAbstractNode" "XMLAbstractNode" ... 
    .. .. ..- attr(*, "class")= chr [1:4] "XMLNode" "RXMLAbstractNode" "XMLAbstractNode" "oldClass" 
    .. ..- attr(*, "class")= chr [1:4] "XMLNode" "RXMLAbstractNode" "XMLAbstractNode" "oldClass" 
    ..$ version : Named list() 
    .. ..- attr(*, "class")= chr [1:5] "XMLCommentNode" "XMLNode" "RXMLAbstractNode" "XMLAbstractNode" ... 
    ..$ children: 
Error in x$children[[...]] : subscript out of bounds 

我在做什麼錯?

+0

沒什麼,可能是。該網址似乎返回一個404錯誤,所以也許它錯了或過時? – joran 2012-04-08 02:47:36

回答

9

該API已被棄用。 Here is the current one。試試這個:

library("RCurl") 
library("XML") 
appid  <- 'ucVVQzLV34GQR4ppLwbdW6G8cCSZDoCBqAc53NXsWB3gXkmP1I4epLwMxboV.PfADi_2ubr2A7Cg8FO4Z3xVxxujza2FJ 8M-' 
address <- paste("11408+Bellflower+Road", "Cleveland", "OH", sep=",+") 
urlRequest <- paste("http://where.yahooapis.com/geocode?q=", 
        address, appid=appid, sep = "") 
doc <- xmlToList(xmlTreeParse(urlRequest)) # Convert to List 
str(doc) 

它爲我工作。雖然,你可能想檢查它是否有正確的地址。有沒有郵政編碼?您可能還需要尋找到Google's Geocoding API。它不再需要煩人的鑰匙。

List of 7 
$ Error  : chr "0" 
$ ErrorMessage: chr "No error" 
$ Locale  : chr "us_US" 
$ Quality  : chr "87" 
$ Found  : chr "1" 
$ Result  :List of 29 
    ..$ quality  : chr "87" 
    ..$ latitude : chr "41.511326" 
    ..$ longitude : chr "-81.605583" 
    ..$ offsetlat : chr "41.511230" 
    ..$ offsetlon : chr "-81.605453" 
    ..$ radius  : chr "2900" 
    ..$ name  : NULL 
    ..$ line1  : chr "11408 Bellflower Rd" 
    ..$ line2  : chr "Cleveland, OH 44106" 
    ..$ line3  : NULL 
    ..$ line4  : chr "United States" 
    ..$ house  : chr "11408" 
    ..$ street  : chr "Bellflower Rd" 
    ..$ xstreet  : NULL 
    ..$ unittype : NULL 
    ..$ unit  : NULL 
    ..$ postal  : chr "44106" 
    ..$ neighborhood: NULL 
    ..$ city  : chr "Cleveland" 
    ..$ county  : chr "Cuyahoga County" 
    ..$ state  : chr "Ohio" 
    ..$ country  : chr "United States" 
    ..$ countrycode : chr "US" 
    ..$ statecode : chr "OH" 
    ..$ countycode : NULL 
    ..$ uzip  : chr "44106" 
    ..$ hash  : chr "BFBDCAB96C2CB175" 
    ..$ woeid  : chr "12776632" 
    ..$ woetype  : chr "11" 
$ .attrs  : Named chr "1.0" 
    ..- attr(*, "names")= chr "version" 
+3

南加州大學(USC)還提供了一個很好的地理編碼服務。它提供了許多谷歌沒有的選項,例如包括人口普查信息(人口普查區域,街區等,地址在其中),並且它提供了很多用於返回對象的選項,例如CSV(一行可以附加到創建一個完整的CSV文件)和KML(在谷歌地球或地圖)的格式觀看。帶有文檔的URL位於https://webgis.usc.edu/Services/Geocode/WebService/GeocoderWebService.aspx – 2012-04-08 03:26:46

+0

謝謝!這真的很棒。我會多玩一下,看看我是否可以進入下一個階段! =) – histelheim 2012-04-08 15:35:42