Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
邮政编码的R包装&;来自IP地址的其他地理数据?_R - Fatal编程技术网

邮政编码的R包装&;来自IP地址的其他地理数据?

邮政编码的R包装&;来自IP地址的其他地理数据?,r,R,我有一个大约200000个IP地址的列表。我想将这些链接到地理位置,并获得IP地址可以提供的任何其他数据 到目前为止,我发现最好的服务是由infochimps提供的: 还有一个面向信息黑猩猩的R软件包。但是infochimps需要你支付200000个IP地址的费用,这可能会很昂贵 有没有R包可以做这样的事情 谢谢尝试使用该软件包,它为数据科学工具包API提供了一个R接口。下面是该软件包作者的一篇文章,应该可以帮助您开始 来自徐旺的评论(搬到这里是为了增加未来的可发现性): 仅供参考:要安装该软件

我有一个大约200000个IP地址的列表。我想将这些链接到地理位置,并获得IP地址可以提供的任何其他数据

到目前为止,我发现最好的服务是由infochimps提供的: 还有一个面向信息黑猩猩的R软件包。但是infochimps需要你支付200000个IP地址的费用,这可能会很昂贵

有没有R包可以做这样的事情

谢谢

尝试使用该软件包,它为数据科学工具包API提供了一个R接口。下面是该软件包作者的一篇文章,应该可以帮助您开始

来自徐旺的评论(搬到这里是为了增加未来的可发现性): 仅供参考:要安装该软件包,必须安装RCurl和rjson。在安装RCurl之前,我必须在Ubuntu上安装两个软件包:
sudo-apt-get-install-curl-libcurl4-gnutls-dev
我需要的函数是
ip2cordinates
,它接受IP地址作为输入

函数IPtoXY()使用相同的API,但不需要额外的软件包

编辑,9月26日: 多亏了@Peter M,我才意识到我的上述功能已不再有效-以下是应该有效的编辑版本(上面的链接也已更新..):

#目的:获取给定IP地址的地理坐标
#作者:凯·奇奇尼
#日期:2011-12-18
#输出:包含经度和纬度的字符串,格式为“X;Y”
IPtoXY来自的功能不起作用

但是这个想法仍然有效,所以这应该可以:

iplocation <- function(ip=""){
  response    <- readLines(paste("http://www.datasciencetoolkit.org//ip2coordinates/",ip,sep=""))
  success     <- !any(grepl("null",response))

  ip <- grep("[[:digit:]]*\\.[[:digit:]]*\\.[[:digit:]]*\\.[[:digit:]]*",response,value=T)
    match <- regexpr("[[:digit:]]*\\.[[:digit:]]*\\.[[:digit:]]*\\.[[:digit:]]*",ip)
    ip <- substr(ip,match,as.integer(attributes(match)[1])+match-1)
  if(success==T){
    extract <- function(label,response){
            text <- grep(label,response,value=T)
            match <- regexpr(paste('"',label,'"',": ",sep=""),text)
            text <- substr(text,match+as.integer(attributes(match)[1]),nchar(text))
            if(grepl("[[:digit:]]",text)){
                    text <- substr(text,1,nchar(text)-2)
            }else{
                    text <- substr(text,2,nchar(text)-2)
                }
            if( regexpr('"',text)!= -1){
                text<-substr(text,2,nchar(text))
            }
            print(text)
            text
        }
  }
  RESULT <- list()
  RESULT$success     <- success
  RESULT$ip          <- ip
  if(success==T){
    RESULT$latitude    <- as.numeric(extract("latitude",response))
    RESULT$longitude   <- as.numeric(extract("longitude",response))
    RESULT$country     <- extract("country_name",response)
    RESULT$locality    <- extract("locality",response)
    RESULT$postalcode  <- extract("postal_code",response)
    RESULT$region      <- extract("region",response)
    RESULT$countrycode <- extract("country_code3",response)
  }
  RESULT
}

iplocation()
iplocation我最近遇到了查找IP地址的问题。我刚刚使用了RCurl库来处理这些:

R> library(RCurl)
R> getURL("http://ipinfo.io/74.125.227.224")
[1] "{\n  \"ip\": \"74.125.227.224\",\n  \"hostname\": \"dfw06s38-in-f0.1e100.net\",\n  \"city\": \"Mountain View\",\n  \"region\": \"California\",\n  \"country\": \"US\",\n  \"loc\": \"37.4192,-122.0574\",\n  \"org\": \"AS15169 Google Inc.\",\n  \"postal\": \"94043\"\n}"
如果只对邮政编码感兴趣,可以修改请求,例如:

R> getURL("http://ipinfo.io/74.125.227.224/postal")
[1] "94043\n"

谢谢,这似乎很有效。仅供参考:要安装该软件包,必须安装
RCurl
rjson
。在安装
RCurl
之前,我必须在Ubuntu上安装两个软件包:
sudo apt get install curl libcurl4 gnutls dev
我需要的功能是
ip2coordinates
,它接受一个IP地址作为输入
R> getURL("http://ipinfo.io/74.125.227.224/postal")
[1] "94043\n"